Javascript Parasitic Modular Inheritance Example detailed

Source: Internet
Author: User
Tags constructor inheritance

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.