This article mainly introduces four methods to implement JavaScript inheritance. This article provides methods to implement JS inheritance, such as prototype chain inheritance, construction inheritance, instance inheritance, and copy inheritance, if you need it, you can refer to JavaScript. Although the inherited keywords are not provided, we can still come up with some good methods.
1. prototype chain inheritance:
The 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:
The 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:
The 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:
The 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:
The 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 ";