Define a parent class:

// Define an animal classfunctionAnimal (name) {/ / attribute this. Name = name | |'Animal'; // Instance method this.sleep =function(){    alert(this.name + 'Sleeping! '); }// Animal.prototype.eat =function(food) {  alert(this.name + 'Eating:'+ food); };Copy the code

1. Prototype chain inheritance

Core: An instance of a parent class as a prototype for a subclass;

function Cat(){}Cat.prototype = new Animal(); Cat.prototype.name ='cat'; //Test Codevar cat = new cat (); alert(cat.name); alert(cat.eat('fish')); alert(cat.sleep()); alert(cat instanceof Animal); //truealert(cat instanceof Cat); //trueCopy the code

Features:

(1) very pure inheritance relationship, instance is an instance of a subclass, but also an instance of a parent class;

(2) The new prototype method/prototype attribute of the parent class can be accessed by all subclasses;

Disadvantages:

(1) To add attributes and methods to a subclass, it must be executed after a statement like new Animal(), not in a constructor;

(2) Multiple inheritance cannot be realized;

(3) Reference properties from the prototype object are shared by all instances;

(4) Cannot pass a parameter to the parent class constructor when creating a subclass instance.

2. Borrow constructor inheritance

Core: Using the constructor of the parent class to enhance the instance of the child class is equivalent to copying the instance properties of the parent class to the child class (without using the stereotype).

function Cat(name){  Animal.call(this);  this.name = name || 'Tom'; } // Test Codevar cat = new Cat(); alert(cat.name); alert(cat.sleep()); alert(cat instanceof Animal); // falsealert(cat instanceof Cat); //trueCopy the code

Features:

(1) Solve the problem of subclass instance sharing parent class reference attribute in 1;

(2) When creating a subclass instance, you can pass parameters to the parent class;

(3) Can implement multiple inheritance (call multiple superclass object) (imperfect, no superclass method)

Disadvantages:

(1) An instance is not an instance of a parent class, but an instance of a subclass;

(2) Only instance attributes and methods of the parent class can be inherited, not the prototype attributes/methods;

(3) Function reuse cannot be realized, each subclass has a copy of the parent class instance function, affecting performance.

3. Combinatorial inheritance

Core: By calling the constructor of the parent class, inherit the attributes of the parent class and retain the advantages of passing parameters, and then inherit the attributes and methods of the parent class by using the parent class instance as the prototype of the subclass to achieve function reuse;

function Cat(name){  Animal.call(this);  this.name = name || 'Tom'; }Cat.prototype = new Animal(); // Test Codevar cat = new Cat(); alert(cat.name); alert(cat.sleep()); alert(cat instanceof Animal); // truealert(cat instanceof Cat); //trueCopy the code

Features:

(1) It makes up for the defect of mode 2 by inheriting both instance attributes/methods and prototype attributes/methods;

(2) both an instance of a subclass and an instance of a parent class;

(3) There is no reference attribute sharing problem;

(4) Can be transmitted;

(5) Function reusable;

(6) Multiple inheritance can be realized (ibid.)

Disadvantages:

(1) Call both parent class constructors, generating two instances (the subclass instance hides the subclass prototype) (only consumes more memory)

4. Inheritance of the original type

Core: Inherit existing objects instead of functions

// Perform a shallow copy of the passed objectfunction object(o){      function F(){}      F.prototype = o;      returnnew F(); }// example var Animal = {name:"cat",      friend:["aaron"."cassic"]}var cat=object(Animal); cat.name="sss"; cat.friend.push("aseaff"); alert(cat.name); alert(cat.friend);Copy the code

Features:

(1) There is no need to create a constructor, just to keep one object similar to another;

(2) As with the archetypal pattern, attribute values containing reference types are shared.

Parasitic inheritance

Core: Closely related to the original type inheritance, but with more methods;

// Perform a shallow copy of the passed objectfunction object(o){      function F(){}      F.prototype = o;      returnnew F(); }// Enhance objectsfunction createobject(o){     var clone=object(o);     clone.sayname=function(){        alert("hi"); };return clone; }// example var Animal = {name:"cat",      friend:["aaron"."cassic"]}var cat=createobject(Animal); cat.sayname();Copy the code

Features:

(1) There is no need to create a constructor, just to keep one object similar to another;

(2) Compared with the original type inheritance, it has its own method;

Disadvantages:

(1) As with original type inheritance, attribute values containing reference types are shared;

6. Parasitic combination inheritance

Core: Create an empty function in a parasitic manner, cut off the instance attributes of the parent class, so that when the constructor of the parent class is called twice, it does not initialize your methods/attributes twice, avoiding the disadvantages of combinative inheritance.

function Cat(name){  Animal.call(this);  this.name = name || 'Tom'; }function aaron(o,y){ var Super = function() {}; Super.prototype=y.prototype; o.prototype=new Super(); o.prototype.constructor=o; }aaron(Cat,Animal); // Test Codevar cat = new Cat(); alert(cat.name); cat.sleep(); alert(cat instanceof Animal); // truealert(cat instanceof Cat); //trueCopy the code

Features:

  1. perfect

Disadvantages:

  1. Implementation is complicated
  2. Cannot implement multiple inheritance (imperfect, ibid.)

# # multiple inheritance

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 Codevar cat = new Cat(); alert(cat.name); cat.sleep(); alert(cat instanceof Animal); // falsealert(cat instanceof Cat); //trueCopy the code

Features:

Support for multiple inheritance

Disadvantages:

Low efficiency, high memory footprint (because you copy the parent’s attributes) (for in allows you to traverse the prototype method)

Use constructors and copies together to achieve multiple inheritance

functionParent1(name,age){ this.name = name; this.age = age; this.height=180; }Parent1.prototype.say =function(){    alert('hi'); }function Parent2(name,age,weight){    this.name = name;    this.age = age;    this.weight = weight;    this.height = 170;    this.skin='yellow'; }Parent2.prototype.walk =function(){    alert('walk'); }functionChild(name,age,weight){ Parent1.call(this,name,age); Parent2.call(this,name,age,weight); }for(var i in Parent1.prototype){Child.prototype[i] = Parent1.prototype[i]}for(var i in Parent2.prototype){Child.prototype[i] = Parent2.prototype[i]}  var c1 = new Child('xiaoming', 10, 8); console.log(c1); //Child { name="xiaoming", age=10, height=170... }console.log(c1.constructor); //Child(name,age,weight)Copy the code

Features:

Javascript multiple inheritance is actually a multiple use of the js circular copy inheritance described earlier

Conclusion:

Javascript can implement multiple inheritance using the Call method and the Prototype attribute. Inheritance methods are similar to single inheritance, except that multiple parent classes need to be inherited in turn. In addition, when attributes or common methods are heavy, the last inherited attributes and methods are the main. Because it overrides the previous inheritance.

The best two inheritance are parasitic combination inheritance and cyclic copy inheritance, but the parasitic combination inheritance can not perfect the realization of multiple inheritance, will miss the parent class prototype method, only cyclic copy inheritance can perfect realization of multiple inheritance.

The original link: www.cnblogs.com/aaronchu/p/…