Four methods to implement inheritance in JavaScript _ javascript skills

Source: Internet
Author: User
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 ";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.