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: