Home >Web Front-end >JS Tutorial >Javascript playing with inheritance (1)_javascript skills
I think everyone has their own opinion on whether Javascript is an object-oriented language or a language that supports objects. Those loyal fans of Javascript must say that Javascript is an object-oriented language. For example, the book "The Return of the King of Javascript" describes Javascript as object-oriented based on prototypes. Let me share my personal opinion. The three characteristics of object-oriented, inheritance, polymorphism, and encapsulation. Although Javascript is not as fast to implement as Java, C# and other object-oriented languages, it still has certain support after all. Therefore, it makes sense to say that Javascript is an object-oriented language. However, from the perspective of inheritance, there are a series of inheritance methods, but each inheritance method cannot realize the power of a true object-oriented language. Therefore, it is said that it is object-oriented. Somewhat far-fetched. To sum up, my understanding of Javascript is that I prefer to call it a simplified object-oriented, or "pseudo" object-oriented (this pseudo word has no derogatory meaning).
Today we will talk about the first feature of object-oriented: inheritance.
What is inheritance? I don’t want to talk nonsense about this. There is an animal, a person, and a girl. This is the simplest and typical inheritance chain.
In object-oriented languages such as C#, it's easy.
So in Javascript, there is no class or inheritance to provide implementation, what should we do?
Object camouflage (construction inheritance method)
What is object camouflage? We might call it structural inheritance to make it easier to understand. As the name suggests, you use constructors to play with inheritance. In fact, it means that the constructor of the parent class is regarded as an ordinary method and is executed in the constructor of the subclass. In this case, when constructing the object, the object of the subclass can of course construct the method of the parent class!
Still using the above example, the code is as follows:
In this case, an inheritance chain is implemented. Test it:
The results are as follows:
a.
b.
c.
d.
e.
f.
Test successful!
Let’s summarize the key points of this code, specify the parent class, declare the parent class object, and then delete the temporary variables. Do you find it troublesome? At least that's what I think. Once you forget to delete, you still have to bear the risk of the parent class being modified. For this, we use call and apply to improve this!
Let’s look at the code next, still the above example (in order to make it easier for everyone to understand, the requirements have been changed, Animal has a name):
Using the same test code, the test was found to be equally successful.
If you are a novice, you may be a little confused by the following two pieces of code. What is call and what is apply? Well, in the topic of playing with inheritance, I have added a supplement series. If you don’t understand this, you can read my article: "Playing with methods: call and apply".
Object camouflage is just a way to achieve inheritance. In the next article, I will continue to write about other inheritance methods and the advantages and disadvantages of several inheritance methods. Welcome to continue to pay attention.