A brief talk on JS inheritance _ Parasitic inheritance & parasitic combined inheritance _javascript skills

Source: Internet
Author: User
Tags inheritance

5. Parasitic inheritance

Like the parasitic constructor and factory pattern, create a function that encapsulates only the inheritance process, which in some way enhances the object and finally returns the object.

function Createanother (original) {
 var clone = Object.create (original);//Create a new object by invoking the functions
 Clone.sayhi = function () {    //To enhance this object
  alert ("Hi") in some way;
 };
 
 return clone;      Returns this object
}

var person = {
 Name: "Bob",
 friends: ["Shelby", "Court", "Van"]
};
var Anotherperson = Createanother (person);
Anotherperson.sayhi ();

In the example above, the Createanother function receives a parameter, which is the object that will be the base of the new object.

Anotherperson is a new object created based on person, and the new object not only has all the attributes and methods of person, but also its own sayhi () method.

6. Parasitic Modular inheritance

Combined inheritance is the most common inheritance mode of JS, and the biggest problem of combinatorial inheritance is that no matter what the case, two constructors are called: once when the subtype prototype is created, and the other one is inside the subtype constructor.

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); The second call to Supertype ()
 
 this.age = Age
;
}  Subtype.prototype = new Supertype (); The first call to Supertype ()
SubType.prototype.sayAge = function () {
 alert (this.age);
}

The first time the Supertype constructor is invoked, Subtype.prototype gets two properties: name and colors; they are all instance properties of Supertype, but are now in the prototype of subtype.

When the subtype constructor is invoked, the Supertype constructor is called again, and this time the instance property name and colors are created on the new object.

These two properties then mask the two attributes of the same name in the prototype.

Parasitic combined inheritance is to solve this problem.

Inheriting attributes by borrowing constructors;

Inherits the method through the prototype chain.

You do not have to call a superclass constructor to specify the stereotype of a subtype.

function Inheritprototype (subtype, supertype) {
 var protoType = object.create (Supertype.prototype);//Create Object
 Prototype.constructor = subtype;     Enhanced Object
 Subtype.prototype = prototype;      The specified 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); The second call to Supertype ()
 
 this.age = Age
;
} Inheritprototype (subtype, supertype)
SubType.prototype.sayAge = function () {
 alert (this.age);
}

var instance = new Subtype ("Bob");
Instance.sayname ();
Instance.sayage ();

The Inheritprototype function receives two parameters: the subtype constructor and the super type constructor.

1. Create a copy of the Super type prototype.

2. Add the constructor property to the created copy to make up for the default constructor property lost by overriding the prototype

3. A prototype that assigns a newly created object (that is, a copy) to a subtype this method invokes only one supertype constructor, and instanceof and isprototypeof () can also be used normally.

The above article on JS inheritance _ Parasitic inheritance & parasitic combination of inheritance is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.