Good programmers share the front line learning simulation in JavaScript object-oriented technology

  Good programmers share the front line learning simulation in JavaScript object-oriented technology, in C # and Java language, object-oriented classes are the way to achieve, especially inherited this characteristic, the way class inheritance showed powerful but also easy to learn. Language is not a pure object-oriented JavaScript, but object-based language, objects that inherit function prototype is in the form of inheritance, when many beginners just getting started with less understanding, but this is implemented as a JavaScript function prototype object-oriented technology, not only feasible, but also provides dynamic inheritance capabilities for object-oriented technology, this article focuses on the JavaScript object-oriented technology. 
I. Overview of the prototype object 
  Each JavaScript object has a prototype object, the object inherits all the properties of the prototype object. Prototype of an object is created by the constructor definition of the object. All JavaScript functions have a prototype called the property, the property reference to the prototype object, the prototype only when the constructor to initialize the object attribute to refer to create the prototype object object. JavaScript does not define the concept of Class class, it defines a class constructor, and initializes the class attribute, each class member will inherit the same properties from prototype object, that is, the prototype object provides an instance of the class of share properties and methods, which saves memory. 
  When reading the properties of an object when, JavaScript objects first look, if not searched, only to find the prototype object attributes (or methods), so, especially for the method, it is best to save the prototype object in order to share, and the purpose of saving memory, and the prototype object also has a powerful, if that is an example of some of the objects in the constructor, the constructor's prototype object to give an increase in properties and methods, it turned out examples of the object instance will inherit these additional properties and methods. 
Second, object attributes, object methods, class attributes, methods based 
  Each object has its own separate copy of the properties and instance methods instance, if an object is instantiated 5, then there will be a copy of the instance attributes and instance methods 5 objects. This keyword refers to instances of their objects, that is, who is operating the instance method, this reference to who; visit the properties which the object instance, this instance to refer to this object. 
  Class methods and class properties is only one copy of the class method invocation must refer to when the name of the class, for example: Date.setHours (); 
  The following examples use a program to show properties, instance methods, class properties, class methods 

 Mobile function (kind, Brand) { 
      this.kind = kind; // definition of the types of mobile phones, such as the GSM / the CDMA 
      this.brand = Brand; // define the brand of the phone, then the this keyword represented by the constructor instantiated Object 
  } 
   
  / //    second step is to define the class definition of the instance method or its other properties in the prototype object constructor     any attribute of the object definition inherit all instances of this class.    // dialing telephone numbers here simply returns    Mobile.prototype.dial = function (PhoneNo) {        return PhoneNo;    };    / 


    
   





   
   
//    third step is to define the class definition of class methods, constants and other necessary class attribute, as a structural property function itself, rather than the constructor     property of a prototype object, attention, class method does not use the keyword this, because they are only the actual parameters of their operation.   // power off method    Mobile.turnOn = function () {       return "of The Power of Mobile IS ON";    }    Mobile.turnOff = function () {  


   






     return "The power of mobile is off"; 
  } 

 
  // class attributes, so they can be used as a constant attention in fact they are not read-only 
  Mobile.screenColor = 64K; // assume all of the class 64K color screen color screen phone 
three subclasses 
   JavaScript support subclass, just handle the class prototype object of a superclass can be instantiated, it should be noted, with such a problem would exist after class technology, because it is a prototype object super class instance subclasses acquired, so it obliterated constructor property provided by the JavaScript itself, in order to ensure the correctness of the constructor, you need to re-specify what the program examples subclass as follows: 
   /   subclass  
// the following is a sub-class constructor intelligent phone 
function the SmartPhone (OS) 

  this.os = OS; 


// Mobile we will object as its prototype 
// This means that the instance of the new class will inherit SmartPhone.prototype, 
// Mobile.prototype inherited by the latter 
in turn by the Object.prototype inherited from //Mobile.prototype 
= new new Mobile SmartPhone.prototype (the GSM, Nokia); 
// add a new method to the following sub-classes, e-mail, where only return Email address 
SmartPhone.prototype.sendEmail = function (emailAddress) { 
  return this.emailAddress 

// the above subclass method a little defective, because we clearly put SmartPhone.prototype set into an object that we have created, so we provide cover JS 
// prototype object, and discarded given Constructor property. The property reference is to create the object constructor. But SmartPhone integrates its target 
// parent class's constructor, it does not own this property, explicitly set with a property can solve this problem: 
SmartPhone.prototype.constructor = SmartPhone; 
var objSmartPhone = new new SmartPhone (); // instantiate Subclass

Guess you like

Origin blog.51cto.com/14479068/2433691