Needless to say, this method is easier to understand, calling the constructor of the parent class in the subclass. In addition, one of the biggest advantages of this method is that structural inheritance can achieve multiple inheritance. Review this code:
But this method also has shortcomings of this and that:
For those who are familiar with object-oriented, let’s take a look at this piece of C# code:
What’s the result? b is of course an instance of A:
However, let’s do this test on the above Javascript code that uses structural inheritance:
But the result was not what we imagined:
In fact, it is easy to explain: structural inheritance only copies the attributes of the parent class by calling the constructor method of the parent class, and does not do anything else, so many materials do not call this inheritance method inheritance.
While seeing the disadvantages, also remember the advantages: supports multiple inheritance.
Let’s look at inheritance in C# and find that there are two most typical differences with this inheritance: C# does not support multiple inheritance, and the shortcomings of structural inheritance I mentioned above. So a new inheritance method emerged, which we called prototypal inheritance.
Seeing the name, you can roughly understand that prototypal inheritance uses the characteristics of prototype to achieve inheritance. This is the most popular inheritance method in Javascript. If you don’t understand prototypes, please follow my other article: "Playing with Prototypes - Prototype".
Let’s look at the code first. Here, I borrow a piece of code from "The Return of the Javascript King":
Test passed. It shows that Point2D has inherited the methods of the parent class. Let’s look at instance again.
alert(p instanceof Point);
Success! Okay, let’s analyze prototypal inheritance:
I won’t go into details about the advantages of prototypal inheritance. The structure is simple, easy to understand, and it can be instanced. But his shortcomings are equally obvious. Do you still remember the example of Animal, People, Girl in my last article? We use prototypal inheritance to achieve this:
Please pay attention to the line of code in my red bold part. People is the prototype of Girl, so we should pass in the name parameter when initializing People, but the name of each Girl is different, so prototype inheritance Occasion 1 when not used: In the prototype inheritance stage, you cannot determine what parameters to use to initialize the parent class object. Occasion 2: Very simple, each class can only have one prototype, which means that prototypal inheritance cannot be used for multiple inheritance. This is a good thing and a bad thing. Because:
Object-oriented languages such as Java and C# do not originally support multiple inheritance, and multiple inheritance is considered to be inconsistent with object-oriented
Cannot implement multiple interfaces!
Okay, that’s all for today. To summarize, Prototype inheritance solves some problems of structural inheritance and introduces some new problems. Generally speaking, prototypal inheritance is the most widely used inheritance method, and it is also the true way to implement inheritance in Javascript grammar!