Saturday 15 March 2014

javascript - B extends A, but B.add populates A.prototype.property -


I have a square and the other that already receives the children of the property.

  function A () {} A.prototype.children = []; Function B () {} B.prototype = new A (); B.Protitip.edbuild = function (hair) {this.children.push (hair); }; Var B = new B (); B.addChild (new object ());   

Strange, to console dump b , there is no item in it. Children (if the property is .baby exists; Chrome / Firefox), but its prototype Children property pops up Why is it so?

There's only an one child's array created in your script , But it is referred to by every context (and even prototype of B) due to heritage.

Instead, give each example your array:

  function A () {this.children = []; }   

And for this, do not create just one array - instead, use

  function B () {A.call (this) Do; // a constructor on this example does everything; b. Prototype = object Build (aprototype); B.Protitip.edbuild = function (hair) {this.children.push (hair); };    

No comments:

Post a Comment