Today, I learned about inheritance in JS, "javascirpt advancedProgramDESIGN: This book is really good. You should have everything you want to learn about Js.
In JavaScript, inheritance can be implemented in four ways,
1. Object impersonating
It seems that this method utilizes the ambiguity of classes and functions in JS. It is also the function keyword declarative method. It can be said that he is a function or class, and JS is too flexible, the following example describes how:
Function Classa (scolor ){
This . Color = Scolor;
This . Saycolor = Function (){
Alert ( This . Color );
}
}
Function Classb (scolor ){
This . Newmethod = Classa; // Assign the classa method to newmethod.
This . Newmethod (); // Call newmethod.
Delete This . Newmethod ;}
When classb executes the classa method, It inherits the classa and deletes the newmethod after the call is completed, because the attributes and methods added later will overwrite the attributes and methods of the superclass if they have the same name as the superclass.
This inheritance method can be used to implement multiple inheritance, such:
Multi-Inheritance
Function Classd (scolor ){
This . Newmethod = Classa; // Assign the classa method to newmethod,
This . Newmethod (); // Call newmethod
Delete This . Newmethod;
This . Newmethod = Classb;
This . Newmethod ();
Delete This . Newmethod ;}
Using this multi-inheritance method has a disadvantage. If classa and classb have the same attributes and methods, classb has a higher priority.
2. Call and apply methods
Because the first method is very popular, ecmascript adds two new methods to the function object, call () and apply (). The two methods are very similar, only in terms of passing parameters,
The first parameter of the Call () method is used as the object of this, for example:
Call Method
Function Classb (scolor, sname ){
Classa. Call ( This , Scolor );
This . Name = Sname;
This . Sayname = Function (){
Alert ( This . Name );
}
}
The call method still calls the classa () method and uses this to pass it to classb to initialize the classb. It only calls the classa () method, if you add a method (for example, the original method) to classa outside of classa, it cannot be inherited to classb.
The first parameter of the Call () method must be this, which can be a second, a third, or a fourth... parameter.
The apply () method differs from the call () method by passing two, third, and fourth parameters... in an array. For example:
Function Classb (scolor, sname, ssex ){
VaR Arr = New Arry (scolor, sname, ssex );
Classa. Apply ( This , Arr); // transmits an array
This . Name = Sname;
This . Sayname = Function (){
Alert ( This . Name );
}
}
You can pass arguments as parameters to apply, but the order of classb parameters must be the same as that of classa.
3. prototype chain
Prototype is used to implement inheritance. Prototype objects are templates. The objects of instances are all based on this template. All its attributes and methods are passed to all instances of that class, prototype chain uses this function to implement the inheritance mechanism.
If the prototype is used to implement inheritance, the example is as follows:
Prototype chain
Function Classa (){
}
Classa. Prototype. Color = " Red " ;
Classa. Prototype. saycolor = Function (){
Alert ( This . Color );
}
Function Classb (){
}
Classb. Prototype = New Classa ();
Through the prototype chain, all attributes and methods of classa are passed to classb, which is indeed much more convenient to use prototype.
Note that the constructor that calls classa does not pass parameters to it, which is a standard practice in the prototype chain. Make sure that the constructor does not have any parameters. What if there are parameters in the constructor? In this way, it cannot be completely inherited. It can only inherit the attributes and methods of the parent class through prototype. The initial attributes and methods in the constructor will not be inherited.
Similar to object impersonating, all attributes and methods of the subclass must appear after the prototype attribute is assigned a value, because all methods assigned before it are deleted. Why? Because the prototype attribute is replaced with a new object, the prototype attribute of the original object does not exist anymore. For example:
Code
Function Classb (){
}
Classb. Prototype = New Classa ();
Classb. Prototype. Name = "" ;
Classb. Prototype. sayname = Function (){
Alert ( This . Name );
}
The disadvantage of the prototype chain is that it does not support multiple inheritance. Remember that the prototype attribute of the class will be overwritten by another type of object.
3. hybrid mode
It is very convenient to use the prototype chain method to implement inheritance. The problem is that constructors with parameters cannot be used. It is the most common and best way to use constructors to define attributes, what can we do?
Combine object impersonating with the prototype chain method, use the object impersonating to inherit the attributes of the constructor, and use the prototype chain to inherit the prototype object method. In this way, rewrite the previous example,CodeAs follows:
Function Classa (scolor ){
This . Color = Scolor;
}
Classa. Prototype. saycolor = Function (){
Alert ( This . Color );
};
Function Classb (scolor, sname ){
Classa. Call ( This , Scolor );
This . Name = Sname;
}
Classb. Prototype = New Classa ();
Classb. Prototype. sayname = Function (){
Alert ( This . Name );
};
Function Show ()
{
VaR A = New Classa ( " Red " );
VaR B = New Classb ( " Blue " , " LHY " );
A. saycolor (); // Output red
B. saycolor (); // Output blue
B. sayname (); // Output LHY
}
In this example, inheritance is implemented in two ways: the call method and the prototype chain. In the constructor, the object impersonates the scolor attribute of the classa class, in the following code, we use the prototype chain to inherit the classa method. This combined with the object impersonating method makes up for the defect that the prototype chain method cannot use constructors with parameters.