When we create an object with a constructor, its properties are added to this. And the properties that are added to this are not actually going to change with the entity, at which point it would seem inefficient. For example:
function her () {
this.name = ' Anna ';
}
This means that every time we create an instance object with new her (), a new name property is generated and has its own storage space in memory. In fact, we can add the name attribute to the prototype so that all instances can share the Name property:
function her () {}
her.prototype.name = ' Anna ';
Thus, when we create an object with new her (), the Name property is no longer a private property of the new object, but is added to the object's prototype. Although this approach can be very efficient, but this is also for the instance object immutable attributes, this is certain otherwise, change this attribute, all the new objects created by this property will be changed, this is not what we want AH ~ ~ ~ The public properties of an object are particularly appropriate for this method.
Next, let's improve an example of the previous one:
function her () {};
Her.prototype.name = ' Anna ';
her.prototype.toString = function () {return
this.name;
}
function his () {};
His.prototype = new Her ();
His.prototype.constructor = his;
His.prototype.sex = ' women ';
As you can see, we have now completed the construction of the related inheritance work before we do the prototype object extension, otherwise the subsequent new attribute methods in His.prototype may erase the inherited things.
function child (f, m) {
this.eat = f;
This.don = m;
}
Child.prototype = new His ();
Child.prototype.constructor = child;
Child.prototype.name = ' Jok ';
Child.prototype.fun = function () {return
this.eat + This.don
}
As you can see, the difference between actually calling ToString () is only a small amount of action behind the scenes. The main difference is the attribute, and the search for methods will occur more frequently in her.prototype.
The above JavaScript will share attributes migrated to the prototype to achieve the method is to share all the content of the small, hope to give you a reference, but also hope that we support the cloud habitat community.