Object inheritance relationships in JavaScript _javascript tips

Source: Internet
Author: User

Let's take a look at the inheritance of class inheritance and the blending of class inheritance and prototype inheritance, called Class inheritance, by using the call or Apply method to impersonate inheritance:

function Desk (size,height) {
this.size=size;
this.height=height;
}
function Mjdesk (size,height) {
desk.call (this,size,height);//This is called Class inheritance.
}
var mj = new Mjdesk (10,123);

Like above this is the class inheritance we want to use, with this inheritance, we can access the methods and properties in the class, but cannot access the methods and properties in the parent prototype, which, as the name suggests, is a false inheritance, so, of course, the fake can not inherit the real prototype, so, The disadvantage of class inheritance is also obvious, when we use more time, it will cause the waste of memory. Thus, we have a hybrid method of class inheritance and prototype inheritance:

function Desk (size,height) {
this.size=size;
this.height=height;
}
function Mjdesk (size,height) {
desk.call (this,size,height);//This is called Class inheritance.
}
Mjdesk.prototype=new Desk ()//prototype Inherits
var MJ = new Mjdesk (12,12);
Of course, the prototype inheritance here is better than using an empty function to inherit in our last chapter.

Of course, the most we are using now is the way to mix the two!

The above is a small set to introduce the JavaScript in the object of the inheritance relationship, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.