Objective
Recently saw JS object-oriented chapter, the main study of the prototype and object-oriented inheritance, in order to comb their own knowledge of the logic, hereby recorded.
JS Object-oriented
Let's talk about the JS creation object method I know now.
1. Write a function and create an object from new
2. The literal Way
The factory method may be used if you want to reuse it.
Factory methods each time you create an object and return
Construct method Create object method can be more concise
However, it is more troublesome to share common methods with implementing instances.
Prototypes can implement instances to share all properties
Each function has a pointer to the prototype object, if the prototype property of a function is assigned to an instance of another function, the prototype of the function will point to the prototype object of another function, thus forming a prototype chain.
The prototype chain can be used to simulate an object-oriented inheritance feature
In the process of prototyping, if the value of a reference type between instances points to the same reference, the values of the reference types between all instances may affect each other.
So introduce the constituent function to generate a replica object to guarantee the independence of the object their combinatorial history is called Combinatorial inheritance way
However, this pattern requires calling the parent class constructor 2 times, and performance may not be the best
At this point, the way of parasitic inheritance comes up. The way that he implements it is to create a copy of the object reference with the object () method, and then assign the replica's constructed instance to the object of the subclass, and finally assign the prototype of the subclass's object to the Copy object, so as to avoid calling the parent class constructor. The parent class is called only in the subclass constructor through the parent class. Call (This,args)
Finally, the parasitic combination inherits the perfect solution to create the object.
JS Object-Oriented inheritance