This article brings you an introduction to various combination inheritance in JavaScript (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
1. Combined inheritance: Also called pseudo-classical inheritance, it refers to an inheritance method that combines prototype chain and borrowed constructor technology.
Let’s 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; } //继承方法 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(); //27
Combined inheritance avoids the shortcomings of the prototype chain and borrowed constructors and combines their advantages.
2. Prototypal inheritance
Inheritance can be implemented without having to define a constructor in advance. Its essence is to perform a shallow copy of a given object. The copied copy can also be further transformed.
function object(o) { function F(){}; F.prototype = o; return new F; } var person = { name: "Nicholas", friends: ["Shelby", "Court", "Van"] }; var antherPerson = object(person); antherPerson.name = "Greg"; antherPerson.friends.push("Rob"); var antherPerson = object(person); antherPerson.name = "Linda"; antherPerson.friends.push("Barbie"); alert(person.friends); //Shelby,Court,Van,Rob,Barbie
3. Parasitic inheritance
is very similar to prototypal inheritance, and is also based on an object or a certain Create an object with this information, then enhance the object, and finally return the object. To solve the inefficiency problem caused by the combined inheritance pattern due to multiple calls to the supertype constructor, this pattern can be used with combined 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 combined inheritance
combines the advantages of parasitic inheritance and combined inheritance. The most efficient way to implement basic type inheritance.
//继承原型 function extend(subType, superType) { function F(){}; F.prototype = superType.prototype; var prototype = new F; prototype.constructor = subType; subType.prototype = prototype; } //超类方法 function SuperType(name) { this.name = name; this.colors = ["red", "blue", "green"]; } SuperType.prototype.sayName = function() { return this.name; } //子类方法 function SubType(name, age) { SuperType.call(this, name); this.age = age; } //继承超类的原型 extend(SubType, SuperType); //子类方法 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); //true
The high efficiency of this example is that it only calls the SuperType constructor once, and therefore avoids creating unnecessary redundancy on SubType.prototype properties. At the same time, the prototype chain can remain unchanged. Therefore, instanceof and isPrototypeOf() can still be used normally. Developers generally believe that parasitic compositional inheritance is the most ideal inheritance paradigm for reference types.
The above is the detailed content of Introduction to various combinations of inheritance in JavaScript (code examples). For more information, please follow other related articles on the PHP Chinese website!