JavaScript inheritance basics, prototype chain, borrowing constructor, mixed mode, prototype inheritance, parasitic inheritance, parasitic combined inheritance

Source: Internet
Author: User
Tags mixed

A good explanation of JavaScript inheritance, but slow to explain now. Don't say much nonsense, go straight to the chase.

Since you want to understand the inheritance, prove that you have a certain understanding of JavaScript object-oriented, such as what is not understood can refer to the "Object-oriented JS basic explanation, Factory mode, constructor mode, prototype mode, mixed mode, dynamic prototype mode", The next step is to complete the JavaScript inheritance through those methods.

 Prototype chain

The simplest way to implement inheritance in JavaScript is to use a prototype chain to Point a prototype of a subtype to an instance of the parent type , which is "subtype. prototype = new parent type ();", the implementation method is as follows:

//Create constructor for parent type function supertype () {this.name = [' Wuyuchang ', ' Jack ', ' Tim ']; This.property = true;} Add a method for the parent type SuperType.prototype.getSuerperValue = function () {return this.property;}    Create constructors for subtypes function subtype () {this.test = [' H1 ', ' H2 ', ' H3 ', ' H4 ']; This.subproperty = false;} To implement the key steps of inheritance, the prototype of the subtype points to an instance of the parent type Subtype.prototype = new supertype ();//To add a method to a subtype here, be sure to do so after inheriting, otherwise you will point the pointer to the instance of the parent type. The method is null SubType.prototype.getSubValue = function () {return this.subproperty;} /* Below is the test code example */var Instance1 = new Subtype () Instance1.name.push (' Wyc '); Instance1.test.push (' H5 '); alert (        Instance1.getsuerpervalue ());            Truealert (Instance1.getsubvalue ());                    Falsealert (Instance1.name);                    Wuyuchang,jack,tim,wycalert (instance1.test);                    H1,h2,h3,h4,h5var Instance2 = new subtype (); alert (instance2.name);                    Wuyuchang,jack,tim,wycalert (instance2.test); H1,h2,h3,h4 

You can see that the code above is a simple inheritance through the prototype chain, but there are still some problems in the test code sample. I believe I've read my blog. "Object-oriented JS basic explanation, Factory mode, constructor mode, prototype mode, mixed mode, dynamic prototype mode" Children's shoes must know that the first problem with the prototype chain code is that the prototype of the subtype is an instance of the parent type, that is, the property of the parent type contained in the prototype of the child type. The prototype property that causes the reference type value to be shared by all instances . The Instance1.name.push (' WYC ') of the above code will prove the existence of this problem. The second problem with the prototype chain is that you cannot pass arguments to a super-type constructor When you create an instance of the subtype . So we seldom use the prototype chain alone in actual development.

borrowing Constructors

To solve two problems in the prototype chain, developers began using a technique called borrowing constructors to solve problems in the prototype chain. The implementation of this technique is also quite simple, just call the constructor of the parent type within the constructor of the sub-type. Remember, a function is simply an object that executes code in a particular environment, so you can execute the constructor by using the Apply () or call () method . The code is as follows:

Creates a constructor for the parent type function supertype (name) {this.name = name;    This.color = [' Pink ', ' yellow '];    This.property = true;    This.testfun = function () {alert (' http://www.cnblogs.com/wuyuchang/'); }}//add a method to the parent type SuperType.prototype.getSuerperValue = function () {return this.property;}    Create constructor function subtype (name) {Supertype.call (this, name) for subtypes;    This.test = [' H1 ', ' H2 ', ' H3 ', ' H4 ']; This.subproperty = false;} To add a method to a subtype here, make sure that after the inheritance is implemented, the pointer will be directed to an instance of the parent type, then the method is null SubType.prototype.getSubValue = function () {return this.subproperty;} /* Below is the test code example */var Instance1 = new Subtype ([' Wuyuchang ', ' Jack ', ' Nick '); Instance1.name.push (' Hello ');                        Instance1.test.push (' h5 '); Instance1.color.push (' Blue '); Instance1.testfun ();                        Http://www.cnblogs.com/wuyuchang/alert (Instance1.name);        wuyuchang,jack,nick,hello//alert (Instance1.getsuerpervalue ());                        Error alert (instance1.test); H1,H2,H3,H4,H5 Alert(Instance1.getsubvalue ());                        False alert (Instance1.color);                        Pink,yellow,bluevar Instance2 = new Subtype (' WYC '); Instance2.testfun ();                        Http://www.cnblogs.com/wuyuchang/alert (Instance2.name);        WYC//Alert (Instance2.getsuerpervalue ());                        Error alert (instance2.test);                H1,h2,h3,h4alert (Instance2.getsubvalue ());                        Falsealert (Instance2.color); Pink,yellow

You can see that the above code is in the constructor of the sub-type subtype by calling the parent type "Supertype.call (this, name);", thus implementing the inheritance of the property, or passing arguments to the parent type when the subtype creates the instance, but the new problem comes again. You can see that I have defined a method in the constructor of the parent type: Testfun, which defines a method in the prototype of the parent type: Getsupervalue. However, after instantiating a subtype, it is still not possible to call the method defined in the prototype of the parent type Getsupervalue, only the method of the constructor in the parent type can be called: Testfun. This is the same as using only the constructor pattern in the Create object, making the function non-reusable. With these problems in mind, the technique of borrowing constructors is seldom used alone.

  

Combinatorial Inheritance (prototype chain + borrowing constructors)

As the name implies, combinatorial inheritance is a pattern combined with the advantages of using a prototype chain and borrowing a constructor function. Implementation is also very simple, since it is a combination, which of course combines the advantages of the two sides, that is , the prototype chain inheritance method, and in the constructor inheritance properties . The specific code is implemented as follows:

Creates a constructor for the parent type function supertype (name) {this.name = name;    This.color = [' Pink ', ' yellow '];    This.property = true;    This.testfun = function () {alert (' http://www.cnblogs.com/wuyuchang/'); }}//add a method to the parent type SuperType.prototype.getSuerperValue = function () {return this.property;}    Create constructor function subtype (name) {Supertype.call (this, name) for subtypes;    This.test = [' H1 ', ' H2 ', ' H3 ', ' H4 ']; This.subproperty = false;} Subtype.prototype = new Supertype ();//To add a method to a subtype here, be sure to do so after inheriting, otherwise when you point the pointer to an instance of the parent type, the method is null SubType.prototype.getSubValue = function () {return this.subproperty;} /* Below is the test code example */var Instance1 = new Subtype ([' Wuyuchang ', ' Jack ', ' Nick '); Instance1.name.push (' Hello ');                        Instance1.test.push (' h5 '); Instance1.color.push (' Blue '); Instance1.testfun ();                        Http://www.cnblogs.com/wuyuchang/alert (Instance1.name);            Wuyuchang,jack,nick,helloalert (Instance1.getsuerpervalue ());                     Truealert (instance1.test);   H1,H2,H3,H4,H5 alert (Instance1.getsubvalue ());                        False alert (Instance1.color);                        Pink,yellow,bluevar Instance2 = new Subtype (' WYC '); Instance2.testfun ();                        Http://www.cnblogs.com/wuyuchang/alert (Instance2.name);            WYC alert (Instance2.getsuerpervalue ());                        Truealert (instance2.test);                H1,h2,h3,h4alert (Instance2.getsubvalue ());                        Falsealert (Instance2.color); Pink,yellow

The above code passes through Supertype.call (this, name), inherits the properties of the parent type, through Subtype.prototype = new Supertype (), and inherits the method of the parent type. The above code is very convenient to solve the prototype chain and borrowing the problems encountered by the constructor, JavaScript is the most common example of the method of inheritance. However, mixed mode is not without drawbacks, you can see in the above code in the inheritance method when the actual inherited property of the parent type, but at this time for the reference type is shared, Therefore, in the constructor of a subtype, calling the constructor of the parent type in order to inherit the property of the parent type overrides the inherited property in the prototype, so it is not necessary to invoke the two constructor, but what can be done to solve it? Look at the following two patterns when solving this problem.

  

  Prototype inheritance

The implementation of the prototype inheritance is different from that of ordinary inheritance, and the prototype inheritance does not use the strict constructor, but the prototype can create the new object based on the existing object without having to create a custom type. The specific code is as follows:

function Object (o) {    function F () {}    f.prototype = O;    return new F ();

code example:

/* Archetypal inheritance */function object (o) {    function F () {}    f.prototype = O;    return new F (); var person = {    name: ' Wuyuchang ',    friends: [' wyc ', ' Nicholas ', ' Tim ']}var Anotherperson = object (person); another Person.name = ' Greg '; AnotherPerson.friends.push (' Bob '); var AnotherPerson2 = object (person); anotherperson2.name = ' Jack '; AnotherPerson2.friends.push (' Rose '); alert (person.friends)    ; Wyc,nicholas,tim,bob,rose

  

Parasitic Inheritance

/* Parasitic inheritance */function createanother (original) {    var clone = object (original);    Clone.sayhi = function () {        alert (' Hi ');    }    return clone;}

Examples of Use:

/* Archetypal inheritance */function object (o) {    function F () {}    f.prototype = O;    return new F ();     /* Parasitic inheritance */function createanother (original) {    var clone = object (original);    Clone.sayhi = function () {        alert (' Hi ');    }    return clone;} var person = {    name: ' Wuyuchang ',    friends: [' wyc ', ' Nicholas ', ' Rose ']}var Anotherperson = createanother (person ); Anotherperson.sayhi ();

  Parasitic combined inheritance

As I said before, the disadvantage of implementing the inheritance in Javascrip is to solve the disadvantage of it, and the realization idea is that, for the constructor inheriting the attribute, and the prototype chain's hybrid form inheriting method, that is, the constructor of the parent type is not instantiated at the time of inheriting the method. The code is as follows:

function Object (o) {    function F () {}    f.prototype = O;    return new F (); /* Parasitic combined inheritance */function inheritprototype (subtype, supertype) {    var prototype = object (Supertype.prototype);    Prototype.constructor = subtype;    Subtype.prototype = prototype;}

In the case of use only the "Subtype.prototype = new Supertype () in the combined mode is required;" This line of code is replaced by Inheritprototype (subtype, supertype); The high efficiency of parasitic combined inheritance is reflected in its invocation of only one parent type constructor, avoiding the creation of unnecessary or superfluous properties. At the same time, the prototype chain remains the same, so instanceof and isprototypeof () can be used normally. This is also the most ideal way of succession, and is now in the transition to this model. (Yui also used this model.) )

This blog post references the 3rd edition of Advanced JavaScript Programming, with the code rewritten, more specific, and annotated to make it easier for everyone to understand. such as the JS inheritance has a unique opinion of the children's shoes do not mean, reply to your opinion for your reference!

JavaScript inheritance basics, prototype chain, borrowing constructor, mixed mode, prototype inheritance, parasitic inheritance, parasitic combined inheritance

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.