1. Prototype chain inheritance

Stereotype attributes containing reference type values are shared by the used instance; You cannot pass arguments to the constructor of a supertype when creating an instance of a subtype (in fact, there is no way to pass arguments to the constructor of a supertype without affecting all object instances);

Basic idea: Use stereotypes to make one reference type inherit the properties and methods of another. The relationship between constructors, stereotypes, and instances: Each constructor has a stereotype object, which contains a pointer to the constructor, and each instance contains an internal pointer to the stereotype object.

function Super(){ this.property = true; } Super.prototype.getSuperValue = function(){ return this.property; } function Sub{ this.subProperty = false; Prototype = new Super(); Sub.prototype.getSubValue(){ return this.subProperty; } var instance=new Sub(); alert(instance.getSuperValue()); // trueCopy the code

2. Borrow constructors

The problem with the constructor pattern is unavoidable — methods are defined in constructors, so function reuse is out of the question. Also, methods defined in the superclass’s prototype are invisible to subclasses, resulting in only the constructor pattern for all types.

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 apply() methods.

function Super(){ this.colors=["red","blue","green"]; } function Sub{// inherit Super super. call(this); } var instance1=new Sub(); instance1.colors.push("black"); alert(instance1.colors); // red,blue,green,balck var instance2=new Sub(); alert(instance2.colors); // red,blue,greenCopy the code

3. Combinatorial inheritance

Basic idea: an inheritance pattern that combines a chain of archetypes with the techniques of borrowing constructors, thereby exploiting the best of both.

Function SuperType(name){this.name = name; this.colors = ['red', 'blue', 'green']; if(typeof this.sayName ! = 'function') {/ / SuperType. Prototype. SayName = function () {/ / / / 'use strict' 'use strict strict mode cannot use' caller, 'callee', and 'arguments' arguments.callee.prototype.sayName = function(){ console.log('name = '+this.name); }; } // subclass SuperType (name, age){// Subclass SuperType (name, age){// Subclass SuperType (name, age); // Call the "superclass" constructor, passing the instance attribute this.age = age; } // subtype.prototype = new SuperType(); Subtype.prototype.constructor = Subtype; Subtype.prototype.sayAge = function() { console.log(this.age); } var instance = new SubType("Nicholas",29);Copy the code

4. Original type inheritance

Basic idea: Prototypes allow you to create new objects based on existing objects, and you don’t have to create custom types as a result.

function object(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"];
};

var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");

var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends); // "Shelby","Court","Van","Rob","Barbie"
Copy the code

ECMAScript5 normalizes old-style inheritance with the addition of the object.create () method, which takes two parameters: an Object to be used as a prototype for the new Object and an Object to define additional properties for the new Object.

Parasitic inheritance

The basic idea: Create a function that just encapsulates the inheritance process, enhances the object internally in some way, and then returns the object as if it really did all the work.

function createAnother(original) {
    var clone = object(original);
    clone.sayHi = function () {
        alert("hi");
    };
    return clone;
}
var person = {
    name:"EvanChen",
    friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();  // "hi"
Copy the code

Parasitic combinatorial inheritance

Basic idea: inherit properties by borrowing functions and inherit methods by mixing forms of prototype chains

function inheritProperty(subType, superType) { var prototype = object(superType.prototype); Prototype. constructor = subType; // Add object subtype. prototype = prototype; } function SuperType(name){this.name = name; this.colors = ["red","blue","green"]; } SuperType.prototype.sayName = function (){ alert(this.name); }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } inheritProperty(SubType, SuperType); SubType.prototype.sayAge = function() { alert(this.age); }Copy the code