Summary of four methods for JavaScript implementation inheritance, javascript4
Although JavaScript does not provide the inherited keywords, we can still come up with some good methods.
1. prototype chain inheritance:
Copy codeThe Code is as follows:
Var Base = function ()
{
This. level = 1;
This. name = "base ";
This. toString = function (){
Return "base ";
};
};
Base. CONSTANT = "constant ";
Var Sub = function ()
{
};
Sub. prototype = new Base ();
Sub. prototype. name = "sub ";
Advantage: From the Perspective of the instanceof keyword, an instance is both a parent class instance and a subclass instance, and seems to be the purest inheritance.
Disadvantage: subclass is different from the attributes and methods of the parent class. It must be executed separately after Sub. prototype = new Base (); and cannot be packaged into the Sub constructor. For example, Sub. prototype. name = "sub"; multi-inheritance cannot be implemented.
2. Construct inheritance:
Copy codeThe Code is as follows:
Var Base = function ()
{
This. level = 1;
This. name = "base ";
This. toString = function (){
Return "base ";
};
};
Base. CONSTANT = "constant ";
Var Sub = function ()
{
Base. call (this );
This. name = "sub ";
};
Advantage: Multi-inheritance can be implemented, and attributes specific to sub-classes can be set inside the constructor.
Disadvantage: instanceof is used to find that the object is not an instance of the parent class.
3. instance inheritance:
Copy codeThe Code is as follows:
Var Base = function ()
{
This. level = 1;
This. name = "base ";
This. toString = function (){
Return "base ";
};
};
Base. CONSTANT = "constant ";
Var Sub = function ()
{
Var instance = new Base ();
Instance. name = "sub ";
Return instance;
};
Advantage: it is the object of the parent class, and the same effect can be obtained if the object is constructed using new and the object is not constructed using new.
Disadvantage: The generated object is only an instance of the parent class, not a subclass object. Multi-inheritance is not supported.
4. Copy inheritance:
Copy codeThe Code is as follows:
Var Base = function ()
{
This. level = 1;
This. name = "base ";
This. toString = function (){
Return "base ";
};
};
Base. CONSTANT = "constant ";
Var Sub = function ()
{
Var base = new Base ();
For (var I in base)
Sub. prototype [I] = base [I];
Sub. prototype ["name"] = "sub ";
};
Advantage: Multi-inheritance is supported.
Disadvantage: inefficient; unable to obtain methods that cannot be enumerated by the parent class.
These forms have their own characteristics. The following table is provided only for the code I provide:
: In addition, if we do not need class inheritance, we only need Object Inheritance. For browsers that support ECMAScript 5, we can also implement it using the Object. create method:
Copy codeThe Code is as follows:
Var Base = function ()
{
This. level = 1;
This. name = "base ";
This. toString = function (){
Return "base ";
};
};
Base. CONSTANT = "constant ";
Var sub = Object. create (new Base ());
Sub. name = "sub ";
How to Implement inheritance in Javascript
Javascript is not a class. Strictly speaking, there is no real inheritance.
Javascript implementation inheritance
Should be similar
Array. prototype. mymethod = function (){
Alert ('mymethod ()');
}
Var B = new Array ();
B. mymethod ();
Override methods in some classes, or define your own methods. For example, the mymethod (); method that does not exist in Array can be used in the current page.
Ah, well, I'm also too lazy to study the specific jquery code .....