Reading notes "JavaScript Object-oriented Programming Guide"
function Shape () {}shape.prototype.name = ' shape '; Shape.prototype.toString = function () {var result = []; if (this.constructor.uber) {Result[result.length] = This.constru Ctor.uber.toString (); } Result[result.length] = THIS.name; Return Result.join (', ');}; function Twodshape () {}var F = function () {}; F.prototype = Shape.prototype; Twodshape.prototype = new F (); TwoDShape.prototype.constructor = Twodshape; Twodshape.uber = Shape.prototype; TwoDShape.prototype.name = ' 2d shape '; function Triangle (side,height) {this.side = side; this.height = height;} var F = function () {}f.prototype = Twodshape.prototype; Triangle.prototype = new F (); Triangle.prototype.constructor = Triangle; Triangle.uber = Twodshape.prototype; Triangle.prototype.name = ' Triangle '; Triangle.prototype.getArea = function () {return THIS.SIDE*THIS.HEIGHT/2;} var my = new Triangle (5, ten); My.tostring ()
-"shape, 2d shape, Triangle"
In the code above: 1, the Uber attribute is set to reference 2 for its parent prototype, and the Tosting () method is updated.
Before that, toString () did nothing but return the contents of this.name, and now adds additional tasks to check for the existence of the This.constructor.uber attribute in the object. If present, the ToString method of the property is called first. This.constructor.uber a reference to the parent prototype of the current object. Thus, when the ToString method of the Triangle entity is called, all ToString on its prototype chain is called.
"Notes" uber--how child objects access the parent object