This article shares the JS object inheritance of n patterns, for everyone to reference.
First, prototype chain inheritance
function person () {};
Person.prototype = {
Constructor:person,
name: "Oliver"
};
function people () {};
People.prototype = new Person ();
People.prototype.constructor = people;
People.prototype.sayName = function () {return
this.name;
};
var ins = new People ();
Console.log (Ins.sayname ());
Second, borrow the constructor (fake object, classic inheritance)
1, no parameters
function supertype () {
this.color = ["Red", "yellow", "white"];
}
Function subtype () {
supertype.call (this);
}
var Instance1 = new subtype ();
var instance2 = new subtype ();
Instance1.color.pop ();
Console.log (Instance1.color); ["Red", "yellow"]
console.log (instance2.color)//["Red", "yellow", "white"
2, has the parameter
function Supertype (name) {
this.name = name;
This.number = [21,32,14,1];
}
Function subtype (name,age) {
supertype.call (this,name);
This.age = age;
}
var Instance1 = new Subtype ("Oliver");
var instance2 = new Subtype ("Troy");
Instance2.number.pop ();
Console.log (instance1.name + instance1.age + instance1.number); oliver1821,32,14,1
Console.log (instance2.name + instance2.age + instance2.number);//troy2421,32,14
Iii. Combinatorial Inheritance (pseudo-classical inheritance)
1, no parameters
function supertype () {
this.color = ["Red", "yellow", "white"];
}
SuperType.prototype.sayColor = function () {return
this.color;
};
Function subtype () {
supertype.call (this);
This.number = 321;
}
Subtype.prototype = new Supertype ();
SubType.prototype.constructor = subtype;
SubType.prototype.sayNumber = function () {return
this.number;
};
var Instance1 = new subtype ();
var instance2 = new subtype ();
Instance2.color.pop ();
Console.log (Instance1.color + instance1.number); red,yellow,white321
Console.log (Instance2.color + instance2.number);//red,yellow321
2, has the parameter
function Supertype (name) {
this.name = name;
This.number = [32,1342,11,1];
}
SuperType.prototype.sayName = function () {return
this.name;
};
Function subtype (name,age) {
supertype.call (this,name);
This.age = age;
}
Subtype.prototype = new Supertype ();
SubType.prototype.constructor = subtype;
SubType.prototype.sayAge = function () {return
this.age;
};
var Instance1 = new Subtype ("Oliver");
var instance2 = new Subtype ("Troy");
Instance2.number.pop ();
Console.log (Instance1.sayname () + instance1.sayage () + instance1.number); oliver1832,1342,11,1
Console.log (instance2.sayname () + instance2.sayage () + instance2.number);//troy2432, 1342,11
Third, parasitic modular inheritance (the most ideal paradigm for reference types)
function Inheritprototype (subtype,supertype) {
var prototype = Object (Supertype.prototype);
Prototype.constructor = subtype;
Subtype.prototype = prototype;
}
function Supertype (name) {
this.name = name;
This.number = [321,321,43];
}
SuperType.prototype.sayName = function () {return
this.name;
};
Function subtype (name,age) {
supertype.call (this,name);
This.age = age;
}
Inheritprototype (subtype,supertype);
SubType.prototype.sayAge = function () {return
this.age;
};
var Instance1 = new Subtype ("Oliver");
var instance2 = new Subtype ("Troy");
Instance2.number.pop ();
Console.log (Instance1.sayname () + instance1.sayage () + instance1.number); oliver18321,321,43
Console.log (instance2.sayname () + instance2.sayage () + instance2.number);//troy24321,321
Or you can write the Inheritprototype function as follows:
function Inheritprototype (subtype,supertype) {
subtype.prototype = new Supertype ();
SubType.prototype.constructor = subtype;
}
Four, prototype inheritance (a value for sharing reference types, similar to a parasitic type)
1, the traditional version (first define the object () function, and then inherit)
function Object (o) {
function F () {};
F.prototype = O;
return new F ();
}
var supertype = {
name: "Oliver", number
: [321,321,4532,1]
};
var SubType1 = object (supertype);
var SubType2 = object (supertype);
Subtype1.name = "Troy";
SubType1.number.pop ();
Subtype2.name = "Alice";
SubType2.number.pop ();
Console.log (subtype1.name + subtype2.name + subtype1.number + subtype2.number + supertype.name + supertype.number); troyalice321,321321,321oliver321,321
ECMAScript version 5 (directly with Object.create (), then inherit)
var supertype = {
name: "Oliver", number
: [321,321,4532,1]
};
var SubType1 = object.create (supertype); The definition Object () function
var SubType2 = object.create (supertype) is omitted;
Subtype1.name = "Troy";
SubType1.number.pop ();
Subtype2.name = "Alice";
SubType2.number.pop ();
Console.log (subtype1.name + subtype2.name + subtype1.number + subtype2.number + supertype.name + supertype.number); troyalice321,321321,321oliver321,321
ECMAScript 5 Short version (defines the second parameter of Object.create (), then inherits)
var supertype = {
name: "Oliver", number
: [321,321,4532,1]
};
var SubType1 = object.create (supertype,{
name: {
value: "Troy"
}
});
var SubType2 = object.create (supertype,{
name: {
value: "Alice"
}
});
SubType1.number.pop ();
SubType2.number.pop ();
Console.log (subtype1.name + subtype2.name + subtype1.number + subtype2.number + supertype.name + supertype.number); troyalice321,321321,321oliver321,321
Parasitic inheritance (values for shared reference types, similar to prototypes)
function Createanother (original) {
var clone = Object (original);
Clone.sayhi = function () {return
"Hi";
};
return clone;
}
var person = {
Name: "Oliver", number
: [13,21,31,1]
};
var Anotherperson = Createanother (person);
AnotherPerson.number.pop ();
Console.log (Anotherperson.sayhi () + anotherperson.number); hi13,21,31
Console.log (person.number);//13,21,31
The above is the entire content of this article, I hope to help you learn.