JavaScript Object-Oriented programming (11) Other ways to inherit

Source: Internet
Author: User

1. to prototype a parent object as a new object

function Object (o) {var n;function f () {}f.prototype = O;n = new F (); n.uber = O;return n;} var shape = {name: ' Shape ', tostring:function () {if (This.uber) return this.uber.toString () + ', ' + this.name;elsereturn thi S.name;}} var Twodshape = object (Shape), Twodshape.name = "2 D shape"; alert (twodshape.tostring ());


2. Call the parent constructor parent.apply () in the sub-constructor and pass the arguments so that the property of the this bound to the parent object becomes the child object at the same time

function Shape (id) {this.id = ID;} Shape.prototype.name = ' shape '; Shape.prototype.toString = function () {return this.name;}; function Triangle () {shape.apply (this, arguments);//native Property inheritance}



JavaScript Object-Oriented programming (11) Other ways to inherit

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.