In front we pave a lot of details, is to let everyone more clear prototype use details;
Now you can combine the previous knowledge and write a function to quickly build an 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 () {}//first inherits, then enhances 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) Quickly build an integrated prototype chain of inheritance relationships