This article is also mentioned before, is purely boring, MO want to take offense. ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~ ~ (~ ̄▽ ̄) ~
As mentioned above, if all the prototype properties point to an identical object, the parent object is affected by the child object, which is not the result we want.
To solve this problem, we need to find a third party, to destroy the ambiguous relationship between them, then we can use an empty function F (), and set her prototype to our parent constructor, and then we can be new F () to create objects that do not contain the parent object properties, That is, the parent property I can no longer set, but also inherit all the properties of the parent object. Although a bit around, see a small example to understand:
functionHer () {};her.prototype.name= ' Anna '; her.prototype.toString=function(){ return This. Name;
functionHis () {};varF =function(){}; F.prototype=Her.prototype;his.prototype=NewF (); His.prototype.constructor=His;his.prototype.name= ' Jock '; His.prototype.sex= ' Men ';
functionChild (width, height) { This. width =width; This. Hieght =height; }varF =function() {}f.prototype=His.prototype;child.prototype=NewF (); Child.prototype.constructor=Child;child.prototype.name= ' Alen ';
In this way, we can keep the prototype chain, (sometimes two people still need a third person to adjust the life, otherwise it will not be boring?) )
My._proto_ = = = Child.prototype; // trueMy._proto_.constructor = = = Child; // true // trueMy._proto_._proto_._proto_.constructor = = = her; // true
The properties of the parent object are not overwritten by the properties of the object.
JavaScript---Temporary builder f ()