Object inheritance relationships in JavaScript (2)

Source: Internet
Author: User

In the previous chapter, we focused on the prototype inheritance relationship between classes in JavaScript: Prototype inheritance is handy for methods in inheriting classes. So let's take a look at the inheritance of class inheritance and the mixing of class inheritance and prototype inheritance, so-called class inheritance, is to use the call or Apply method to impersonate the inheritance:

1 functionDesk (size,height) {2      This. size=size;3       This. height=height;4 }5 functionMjdesk (size,height) {6Desk.call ( This, size,height);//This is called Class inheritance.7 }8  varMJ =NewMjdesk (10,123);

Like the above 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, this method alias pseudo-inheritance, as the name implies, is a false inheritance, so, the false certainly can not inherit the real prototype, so, The disadvantage of class inheritance is also obvious, when we use a lot of time, it will create a waste of memory. Thus, we have a hybrid approach to class inheritance and prototype inheritance:

1 functionDesk (size,height) {2       This. size=size;3       This. height=height;4  }5  functionMjdesk (size,height) {6Desk.call ( This, size,height);//This is called Class inheritance.7  }8Mjdesk.prototype=NewDesk ();//prototype Inheritance9 varMJ =NewMjdesk (12,12);Ten //Of course, the prototype inheritance here is better in the way that we use an empty function to inherit from the previous chapter.

Of course, what we're using most now is the combination of these two ways!

Object inheritance relationships in JavaScript (2)

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.