This time we will talk about combination, prototype, parasitic, and parasitic combination inheritance methods.
1. Combination inheritance : also known as pseudo-classical inheritance, it refers to an inheritance method that combines prototype chains and borrowed constructor technologies into one piece.
Let’s take a look at an example:
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; } //Inheritance method SubType.prototype = new SuperType(); SubType.prototype.sayAge = function() { alert(this.age); } var instance1 = new SubType("Nicholas", 29); instance1.colors.push("black"); alert(instance1.colors); //red,blue,green,black instance1.sayName(); //Nicholas instance1.sayAge(); //29 var instance2 = new SubType("Greg", 27); alert(instance2.colors); //red,blue,green instance2.sayName(); //Greg instance2.sayAge(); //27Combination inheritance avoids the shortcomings of prototype chains and borrow constructors, and integrates their advantages.
2. Prototype inheritance
Inheritance can be implemented without predefined constructors, essentially performing shallow copying of a given object. The copied copy can be further modified.
function object(o) { function F(){}; F.prototype = o; return new F; } var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = object(person); anotherPerson.name = "Greg"; anotherPerson.friends.push("Rob"); var anotherPerson = object(person); anotherPerson.name = "Linda"; anotherPerson.friends.push("Barbie"); alert(person.friends); //Shelby,Court,Van,Rob,Barbie3. Parasitic inheritance
Very similar to prototype inheritance, it is also to create an object based on an object or some information, then enhance the object, and finally return the object. To solve the inefficiency problem caused by the combination inheritance pattern due to multiple calls to the supertype constructor, this pattern can be used with combination inheritance.
function object(o) { function F(){}; F.prototype = o; return new F; } function createAnother(original) { var clone = object(original); clone.sayHi = function() { alert("Hi"); }; return clone; } var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var anotherPerson = createAnother(person); anotherPerson.sayHi();4. Parasitic combination inheritance
Combining the advantages of parasitic inheritance and combination inheritance is the most effective way to achieve basic type inheritance.
//Inheriting prototype function extend(subType, superType) { function F(){}; F.prototype = superType.prototype; var prototype = new F; prototype.constructor = subType; subType.prototype = prototype; } //Superclass method function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { return this.name; } //Subclass method function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { return this.name; } //Subclass method function SubType(name, age) { SuperType.call(this, name); this.age = age; } //Prototype extend(SubType, SuperType); //Subclass method SubType.prototype.sayAge = function() { return this.age; } var instance1 = new SubType("Shelby"); var instance2 = new SubType("Court", 28); instance1.colors.push('black'); alert(instance1.colors); //red,blue,green,black alert(instance2.colors); //red,blue,green alert(instance1 instanceof SubType); //true alert(instance1 instanceof SuperType); //trueThe efficiency of this example is reflected in that it calls the SuperType constructor only once, and thus avoids creating unnecessary unnecessary properties on SubType.prototype. At the same time, the prototype chain can remain unchanged. Therefore, instanceof and isPrototypeOf() can also be used normally. Developers generally believe that parasitic combinatorial inheritance is the most ideal inheritance paradigm for reference types.
The above article JS object-oriented inheritance--a detailed explanation of multiple combination inheritance is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.