Inheritance in javascript-parasitic combined inheritance

Source: Internet
Author: User

As mentioned above, composite inheritance is the most common inheritance mode in javascript. However, it also has its own shortcomings: Composite inheritance calls two parent class constructor times no matter under what circumstances, one is when the subclass prototype is created, and the other is inside the subclass constructor. the subclass will eventually contain all instance attributes of the parent class object, but we have to override these attributes when calling the subclass constructor. let's take another example of combined inheritance: copy the code function SuperType (name) {this. name = name; this. friends = ["gay1", "gay2"];} SuperType. prototype. sayName = function () {alert (this. name) ;}; funciton 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) ;}; copy the SubType when the SuperType constructor is called for the first time. prototype has two attributes: name and friends. They are both SuperType instance attributes. it is only in the prototype of SubType. when the SubType constructor is called, The SuperType constructor is called again. This time, the instance property name and friends are created on the new object. therefore, these two attributes shield the two attributes with the same name in the prototype. the result is that there are two groups of name and friends attributes, one on the SubType instance and the other on the SubType prototype. this is the result of calling the SuperType constructor twice. now we have found a solution to this problem: parasitic combined inheritance. parasitic combined inheritance: inherits attributes by borrowing constructor and inherits methods by means of the prototype chain. idea: You don't have to call the constructor of the parent class to specify the prototype of the Child class. All we need is a copy of the prototype of the parent class. essentially, it uses parasitic inheritance to inherit the prototype of the parent class, and then specify the prototype of the result to the subclass: function inheritPrototype (subType, superType) {var prototype = object (superType. prototype); // create a copy of the parent class prototype is equivalent to using the Object. create (superType. prototype) prototype. constructor = subType; // Add the constructor attribute to the replica to compensate for the subType of the constructor attribute lost when the prototype is rewritten. prototype = prototype; // assign the created object (copy) to the prototype of the subclass} so that we can call the inheritPrototype () function, replace the assignment statement for the subclass prototype in the previous example: copy the code function inheritPrototype (subType, superType) {var prototype = Object. create (superType. prototype); // create a copy of the parent class prototype is equivalent to using the Object. create (superType. prototype) prototype. constructor = subType; // Add the constructor attribute to the replica to compensate for the subType of the constructor attribute lost when the prototype is rewritten. prototype = prototype; // assign the created object (copy) to the prototype of the subclass} function SuperType (name) {this. name = name; this. friends = ["gay1", "gay2"];} SuperType. prototype. sayName = function () {alert (this. name) ;}; function SubType (name, age) {SuperType. call (this, name); // inherits SuperType this. age = age; // extended age attribute} inheritPrototype (SubType, SuperType); SubType. prototype. sayAge = function () {alert (this. age) ;}; // extend the sayAge method var person1 = new SubType ("nUll", 25); var person2 = new SubType ("mywei", 25); person1.friends. push ("gay3"); person1.sayName (); person1.sayAge (); alert (person1.friends); // gay1, gay2, gay3alert (person2.friends); // gay1, gay2alert (person1 instanceof SubType); // truealert (person1 instanceof SuperType); // truealert (SubType. prototype. isPrototypeOf (person1); // truealert (SuperType. prototype. isPrototypeOf (person1); // The efficiency of copying the code in this example is that it only calls the SuperType constructor once, and thus avoids the SubType. create unnecessary properties on prototype. at the same time, the prototype chain remains unchanged. therefore, you can use instanceof and isPrototypeOf to determine the inheritance relationship.

Related Article

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.