JS several inheritance methods (six kinds)

Source: Internet
Author: User

Several ways to realize the inheritance of JS
Most languages support two types of inheritance: interface inheritance and implementation inheritance, and interface inheritance cannot be implemented in JavaScript.
JavaScript only supports implementation of inheritance, and its implementation relies primarily on the prototype chain to implement.
The main ways of inheriting are as follows:
1. Prototype chain inheritance
2. Constructor inheritance
3. Instance Inheritance
4. Copy Inheritance
5. Combination Inheritance (prototype chain inheritance + constructor inheritance)
6. Parasitic combined inheritance

How to implement JS inheritance
Since we want to implement inheritance, first we have to have a parent class, the code is as follows:

Define an animal class
function Animal (name) {
Property
this.name = name | | ' Animal ';
Instance method
this.sleep = function () {
Console.log (THIS.name + ' sleeping! ');
}
}
Prototyping methods
Animal.prototype.eat = function (food) {
Console.log (this.name + ' eating: ' + food);
};

first, the prototype chain inheritance
Core: Prototype an instance of a parent class as a child class
Basic idea: Use a prototype to have a reference type inherit the properties and methods of another reference type.
Constructors, prototypes, relationships between instances: each constructor has a prototype object, and the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object.
function Cat () {}
Cat.prototype = new Animal ();
Cat.prototype.name = ' Cat ';

Test Code
var cat = new Cat ();
Console.log (cat.name); //cat
cat.eat (' fish '); //cat is eating: Fish
Cat.sleep (); //cat is sleeping!
Console.log (cat instanceof Animal); //true
Console.log (cat instanceof cat); //true
Characteristics:
A very purely inherited relationship, an instance of a subclass, and an instance of the parent class.
The parent class adds a new prototype method/prototype property that the subclass can access to
Simple and easy to implement
Disadvantages:
Unable to implement multiple inheritance
The reference property from the prototype object is shared by all instances
Cannot pass parameter to parent class when creating child class instance
Second, the construction of inheritance
Core: Use the constructor of the parent class to enhance the subclass instance, which is equivalent to copying the instance property of the parent class to the subclass (useless to the prototype)
Basic idea: The superclass constructor is called inside the subtype constructor, and the constructor can be executed on the newly created object by using the call () and the Apply () methods.

function Cat (name) {
Animal.call (this);
this.name = name | | ' Tom ';
}
Test Code
var cat = new Cat ();
Console.log (cat.name); //tom
Cat.sleep (); //tom is sleeping .
Console.log (cat instanceof Animal); //False
Console.log (cat instanceof cat); //True
cat.eat (' fish '); //Error
Characteristics:
Resolves an issue where subclass instances share parent class reference properties in 1
When you create a child class instance, you can pass parameters to the parent class
Multiple inheritance can be implemented (call multiple parent objects)
Disadvantages:
The instance is not an instance of the parent class, just an instance of the child class
Only instance properties and methods of the parent class can be inherited and cannot inherit the prototype properties/methods
Function reuse is not possible, each subclass has a copy of the parent class instance function, which affects performance
third, instance inheritance
Core: Adds a new attribute to the parent class instance, returning as a subclass instance

function Cat (name) {
var instance = new Animal ();
instance.name = name | | ' Tom ';
return instance;
}
Test Code
var cat = new Cat ();
Console.log (cat.name) //tom
Cat.sleep (); //tom is sleeping .
cat.eat (' fish '); //Tom is eating: Fish
Console.log (cat instanceof Animal); //True
Console.log (cat instanceof cat); //False
Characteristics:
Does not restrict invocation, whether it is a new subclass () or a subclass (), the returned object has the same effect
Disadvantages:
Instance is an instance of the parent class, not an instance of the child class
Multiple inheritance is not supported
iv. Copy Inheritance
function Cat (name) {
var animal = new Animal ();
For (var p in animal) {
cat.prototype[p] = animal[p];
}
Cat.prototype.name = name | | ' Tom ';
}

Test Code
var cat = new Cat ();
Console.log (cat.name) //tom
Cat.sleep (); //tom is sleeping .
cat.eat (' fish '); //tom is eating: Fish
Console.log (cat instanceof Animal); //False
Console.log (cat instanceof cat); //True
Characteristics:
Support Multiple inheritance
Disadvantages:
Low efficiency and high memory consumption (because you want to copy the properties of the parent class)
Unable to get parent class non-enumerable method (non-enumerable method, cannot be accessed using for in)
v. Combination inheritance
Core: By calling the parent class construct, inheriting the property of the parent class and preserving the advantages of the pass parameter, and then implementing the function reuse by using the parent class instance as the subclass prototype
function Cat (name) {
Animal.call (this);
this.name = name | | ' Tom ';
}
Cat.prototype = new Animal ();
Cat.prototype.constructor = Cat;

Test Code
var cat = new Cat ();
Console.log (cat.name) //tom
Cat.sleep (); //tom is sleeping .
cat.eat (' fish '); //tom is eating: Fish
Console.log (cat instanceof Animal); //True
Console.log (cat instanceof cat); //True
Characteristics:
compensate for the defect in mode 2, can inherit instance properties/methods, or inherit prototype properties/Methods
is both an instance of a subclass and an instance of the parent class
There is no reference attribute sharing issue
Can be passed the parameter
Functions can be reused
Disadvantages:
A two-time parent constructor was called, and two instances were generated (the subclass instance masked the sub-class prototype)
VI. Parasitic combination inheritance
Core: By parasitic way, the instance property of the parent class is cut off, so that when two times the construction of the parent class is called, the two instance method/property is not initialized, the disadvantage of avoiding the combination inheritance
function Cat (name) {
Animal.call (this);
this.name = name | | ' Tom ';
}
(function () {
//Create a class with no instance method  
var Super = function () {};
super.prototype = Animal.prototype;
//prototype The instance as a subclass 
Cat.prototype = new Super ();
})();

Test Code
var cat = new Cat ();
Console.log (cat.name) //tom
Cat.sleep (); //tom is sleeping .
cat.eat (' fish '); //tom is eating: Fish
Console.log (cat instanceof Animal); //True
Console.log (cat instanceof cat);//true

Cat.prototype.constructor = Cat; //need to fix the next constructor
Characteristics:
Perfect
Disadvantages:
Achieve more complex

Reference link: https://www.cnblogs.com/humin/p/4556820.html;
Reference Link: https://www.jb51.net/article/81766.htm

JS several inheritance methods (six kinds)

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.