We had a lot of detail in front of us. In order to make everyone more clear about the use of prototype details;
Now it is possible to integrate the previous knowledge and write a function for high-speed construction of the inheritance relationship based on the prototype chain:
function extend (child, Parent) {var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F (); Child.prototype.constructor = child; Child.uber = Parent.prototype;}
It is also very easy to use:
function Shape () {}//augment prototypeShape.prototype.name = ' shape '; Shape.prototype.toString = function () {var result = [];if (this.constructor.uber) {Result[result.length] = This.constructor.uber.toString ();//super.tostring ()}result[result.length] = This.name;return result.join (', ');}; function Twodshape () {}//inherits first. Re-enhancement extend (twodshape,shape); TwoDShape.prototype.name = ' 2D shape '; function Triangle (side, height) {this.side = Side;this.height = height;} Extend (Triangle,twodshape); Triangle.prototype.name = ' Triangle ';//Use the inherited ToString Method alert (new Triangle (10,5). ToString ());
JavaScript Object-Oriented programming (9) high-speed construction of the integrated prototype chain of inheritance relationship