Talking about JS inheritance-parasitic inheritance & parasitic combined inheritance, talking about js inheritance parasitic

Source: Internet
Author: User

Talking about JS inheritance-parasitic inheritance & parasitic combined inheritance, talking about js inheritance parasitic

5. Parasitic inheritance

Similar to the parasitic constructor and the factory mode, create a function that only encapsulates the inheritance process. This function enhances the object in some way internally and returns the object.

Function createAnother (original) {var clone = Object. create (original); // create a new object clone by calling the function. sayHi = function () {// enhance the alert ("Hi") ;}; return clone; // return this object} var person = {name: "Bob", friends: ["Shelby", "Court", "Van"]}; var anotherPerson = createAnother (person); anotherPerson. sayHi ();

In the preceding example, the createAnother function receives a parameter, that is, the object to be used as the base of the new object.

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

6. Parasitic combined inheritance

Combination inheritance is the most common inheritance mode in js. The biggest problem with combination inheritance is that, under whatever circumstances, two constructor calls: one is when a child Type prototype is created, the other is in the Child Type 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 of SuperType () this. age = age;} SubType. prototype = new SuperType (); // call SuperType () SubType for the first time. prototype. sayAge = function () {alert (this. age );}

When the SuperType constructor is called for the first time, SubType. prototype will obtain two attributes: name and colors. They are both SuperType instance attributes, but are now in the SubType prototype.

When the SubType constructor is called, The SuperType constructor is called again. This time, the instance property name and colors are created on the new object.

Therefore, these two attributes shield the two attributes with the same name in the prototype.

Parasitic combined inheritance is designed to solve this problem.

Inherit attributes by borrowing constructors;

Use the prototype chain to inherit methods.

You do not have to call a super-Type constructor to specify a sub-type prototype,

Function inheritPrototype (subType, superType) {var protoType = Object. create (superType. prototype); // create the protoType object. constructor = subType; // enhancement object subType. prototype = protoType; // specify the 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 of SuperType () this. age = age;} inheritPrototype (SubType, SuperType) SubType. prototype. sayAge = function () {alert (this. age);} var instance = new SubType ("Bob", 18); instance. sayName (); instance. sayAge ();

The inheritPrototype function receives two parameters: subtype constructor and subtype constructor.

1. Create a copy of the super-type prototype.

2. Add the constructor attribute to the created copy to compensate for the default constructor attribute lost due to the rewriting of the prototype.

3. assign a new object (I .e. a copy) to the prototype of the Child type. This method only calls the SuperType constructor once, And instanceof and isPrototypeOf () can be used normally.

The above discussion about JS inheritance _ parasitic inheritance & parasitic combined inheritance is all the content shared by the editor. I hope to give you a reference and support for the house of helping customers.

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.