This time to talk about combination, prototype, parasitic, parasitic combination of inheritance.
1. Combinatorial inheritance : Also known as pseudo-classical inheritance, refers to the combination of prototype chain and borrowed constructor technology in a way of inheritance.
Here's 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");
Instance1.colors.push ("Black");
alert (instance1.colors); Red,blue,green,black
instance1.sayname ();//nicholas
instance1.sayage ();//29
var instance2 = new Subtype ("Greg");
alert (instance2.colors); Red,blue,green
instance2.sayname ();//greg
instance2.sayage ();//27
Combinatorial inheritance avoids the defects of prototype chains and borrowed constructors, and incorporates their advantages.
2. Prototype inheritance
You can implement inheritance without having to define a constructor, essentially by performing a shallow copy of the given object. The copy can also be further modified.
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
Very similar to archetypal inheritance, you create an object based on an object or some information, then enhance the object, and finally return the object. This pattern can be used in conjunction with composite inheritance in order to resolve the inefficiency caused by the multiple invocation of the superclass constructor by the composite inheritance pattern.
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 Modular inheritance
The advantages and benefits of parasitic inheritance and combination inheritance are the most effective ways 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;
}//Super class 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;
}//Inherit the superclass 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); True
The high efficiency of this example is reflected in its call to the Supertype constructor only once, and thus avoids creating unnecessary unwanted attributes on the subtype.prototype. At the same time, the prototype chain can remain unchanged. Therefore, instanceof and isprototypeof () can also be used normally. It is generally accepted by developers that parasitic combined inheritance is the most ideal inheritance paradigm for reference types.
The above JS object-oriented inheritance---a variety of combination of inheritance is a small set to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.