Learning JavaScript design patterns (inheritance) _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly guides you through the JavaScript design model. It focuses on inheritance, illustrates why inheritance is required, and analyzes inheritance in detail. If you are interested, refer to it. 1. Inheritance

Inheritance in javascript is a very complex topic, and it is much more complex than any other object-oriented language. In most other object-oriented languages, only one keyword is required to inherit a class. Different from them, javascript requires a series of measures to inherit the public members. What's more, javascript is one of the few languages that use the original type inheritance. Benefiting from the flexibility of this language, you can either use standard class-based inheritance or use more subtle original type inheritance.

2. Why inheritance?

In general, when designing a class, we want to reduce repetitive code and minimize the coupling between objects. Use inheritance to meet the needs of the previous design principles. With this mechanism, you can design based on existing classes and make full use of the methods they already have, making it easier to modify the design. Suppose you need to make several classes have a toString () method that outputs the class structure in a specific way. Of course, you can use the copy and paste method to define toString () the code of the method is added to every class, but in this way, you will have to repeat the same modification in every class whenever you need to change the way this method works. If you provide a ToStringProvider class and then let those classes inherit the class, you only need to declare the toString method in one place.

Inheriting a class from another class may lead to strong coupling between the two, that is, one class depends on the internal implementation of another class. We will discuss some technologies that help avoid this problem, including the techniques that provide methods for other classes by using the metadata class.

3. class-based inheritance

The following code is used:

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.