JavaScript Object-Oriented programming (9) high-speed construction of the integrated prototype chain of inheritance relationship

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.