On, the relationship between the instance property / method constructor JS prototype object (the prototype)

Original link: https://segmentfault.com/a/1190000016951069

Constructor Function: A function, you can create an instance of the keyword new. For convenience of distinction, generally capitalized;
prototype object: a special object, automatically generated constructor Create; forming one correspondence with the constructor, and the like shadowy relationship;
Example: Example of objects out of the constructor ;


  When defining constructors, properties and methods defined in the inside thereof ( "{" and "}"). When we, for instance constructor of the keyword new. Examples of these properties have a copy constructor copy, then attributed to the current instance. Properties and methods among the different instances are completely independent, the following examples:

function Person(name,age){
    this.name = name;
    this.age = age;
    this.sayName = function(){
           alert(this.name);
    };
}
 
var HL = new Person("hailing",18);
var JJ = new Person("jiajia",20);
 
HL.sayName(); // hailing
JJ.sayName(); // jiajia
 
HL.name = "HAILING";
 
HL.sayName (); // Hailing 
JJ.sayName (); // JIAJIA

  We instantiate two objects "HL", "JJ", when we change the name "HL" of, "JJ" name will not be changed by the creation of a new Person.

  When the program is executed, automatically generates a prototype object (the prototype) associated therewith. And automatically add a property to the constructor: prototype, this property is a pointer to the prototype object. At the same time, add a property to the prototype object: constructor, the property is also a pointer to the corresponding constructor. At this point, the prototype object will only include these default methods and properties.

  After completion of the Examples, all instances of implicit form many-relationship with the prototype object. All examples share prototype object properties and methods, of course, includes constructor. When the prototype object is added a property or method, it will be shared by all instances, which can be accessed by any one instance. If the method or attribute property or method prototype object instance names are consistent with, the examples of its properties or methods priority over the prototype object:

// add public properties and methods 
Person.prototype.sex = "Girl" ;
Person.prototype.saySex = function (){
   alert(this.sex);
};
alert(HL.saySex === JJ.saySex); // true
alert(HL.saySex === Person.prototype.saySex); // true
 
Person.prototype.stature = 165;
alert(HL.stature); // 165
alert(JJ.stature); // 165
 
// an instance of an object modify or add private property. 
= 160. HL.stature ;
alert(HL.stature); // 160
alert(JJ.stature); // 165
 
// delete the object instance private property. 
the Delete HL.stature;
Person.prototype.stature = 170;
alert(HL.stature); // 170
alert(JJ.stature); // 170

  Example, the prototype object to increase sex saySex properties and methods, this time "HL" and "JJ" to access the object by way of example and compared, and also the prototype object itself saySex compared result ture; prototype then gave Object attributes increased stature, this time "HL" and "JJ" in this is not the property, so that access to the properties of the prototype object stature. When we are alone to "HL" adds stature property after, "HL" is not the prototype object access, but access to "HL" own the property. When we remove the "HL" own properties by keyword "delete", when accessed again, he returned to revisit the property on the prototype object.


Summary: constructor properties and methods declared and defined only once instantiated completed. Examples of object's own properties and methods constructor will not there is any relationship. Example Object prototype object to form a "spare tire" relationship, when accessed by an object property or method, the program object itself preferentially searches property or method, the method or attribute is accessed only prototype object does not exist.

  Accordingly, when multiple instances of the same need to use a property or method, this method should we put in the prototype object, property or method to avoid the same times in a plurality of objects exist independently waste memory.

 

Guess you like

Origin www.cnblogs.com/wangjing-web/p/11917041.html