Contacted several inheritance patterns, in the previous article, we talked about some inheritance methods, this thought the combination is the best, the results did not expect to have.
The story starts from the archetypal inheritance:
The code is as follows |
Copy Code |
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 Yetanotherperson = object (person); Yetanotherperson.name = "Linda"; YetAnotherPerson.friends.push ("Barbie"); Console.log (Person.friends);
|
Prototype inheritance seems to be slightly like prototype's case, output as [' Shelby ', ' Court ', ' Van ', ' Rob ', ' Barbie ']
We can then use our own object.create () to replace the defined function.
Parasitic inheritance also uses this feature
The code is as follows |
Copy Code |
function Createanother (original) { var clone = Object.create (original); Clone.sayhi = function () { Console.log ("Hi"); }; return clone; } var person = { Name: "Nicholas", Friends: ["Shelby", "Court", "Van"] }; var Anotherperson = Createanother (person); Anotherperson.sayhi ();
|
What does not understand is why the sayhi defined here cannot be shared to person.
And parasitic modular inheritance now seems to be called the most efficient way (-_-actually I don't understand)
code is as follows |
copy code |
function Inheritprototype (subtype, supertype) { var prototype = object.create (Supertype.prototype); Prototype.constructor = subtype; Subtype.prototype = prototype; } Function supertype (name) { THIS.name = name; This.color = ["Red", "Blue", "green"]; } SuperType.prototype.sayName = function () { Console.log (this.name); }; Function Subtype (name, age) { Supertype.call (this, name); This.age = age; } Inheritprototype (subtype, supertype); SubType.prototype.sayAge = function () { Console.log (this.age); }; |
Add a test example
The code is as follows |
Copy Code |
<title>parasitic Combination Inheritance Example</title> <script type= "Text/javascript" > function Object (o) { function F () {} F.prototype = O; return new F (); } function Inheritprototype (subtype, supertype) { var prototype = object (Supertype.prototype); Create Object Prototype.constructor = subtype; Augment object Subtype.prototype = prototype; Assign Object } 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; } Inheritprototype (subtype, 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 </script> <body> </body>
|