Inheritance of JavaScript classes (recommended)

Source: Internet
Author: User
In fact, I saw the implementation of inheritance when I first learned JavaScript. At that time, I just tried to understand the code snippet from the book. Today, I think again. It seems that this is the result of the evolution of thinking exploration. Inheritance, that is, reuse. If we leave the inherent idea of inheritance aside and allow B to reuse members of a, the simplest and most crude practice is ba; then the problem arises: any changes to B, it's just a change to a (the same object). In fact, when I first learned JS, I saw the implementation of inheritance. At that time, I just tried to understand the code snippet from the book. Today, I think again. It seems that this is the result of the evolution of thinking exploration.

Inheritance, that is, reuse.

If we leave aside the inherent idea of inheritance and allow B to reuse members of a, the simplest and most crude practice is B =;

So the question is: any change to B is a change to a (the same object ).

Okay, copy one copy. If the shortest copy is not secure enough, use the deep copy.

Problem: code is reused, but memory is wasted (both variables and methods are objects in JS ).

Do not copy, read-only, do not write, you can use the JS prototype, B. proto =. Generally, we do not directly change proto, which is too violent. JS provides a method that can be "mild" to achieve the goal-Object. create (B ).

The above is the most basic inheritance. How can a subclass more commonly call the constructor and members of the parent class (such as this. _ super), how to more commonly implement the inheritance mode (such as A = inheritFrom (B), and so on, not in the scope of this article ^ O ^

The above is a detailed description of the Inheritance (recommended) of JavaScript classes. For more information, see other related articles in the first PHP community!

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.