"JavaScript review" inheritance

Source: Internet
Author: User

Combining Inheritance

  Combinatorial inheritance (combination inheritance), sometimes referred to as pseudo-classical inheritance, refers to the combination of the prototype chain and the technique of borrowing the constructor into a piece, thus exerting a succession pattern of both. The idea behind it is to use the prototype chain to implement the inheritance of the prototype properties and methods, and to implement the inheritance of the instance properties by borrowing the constructor function. This enables the reuse of functions both by defining methods on the prototype and ensuring that each instance has its own properties. Let's look at an example:

  

<script>function person (name, age, friends) { This. Name =name;  This. Age =Age ;  This. Friends = ["Carol","Seylia"]; if(typeof  This. Showperson! ="function") {                 This. Showperson =function () {varMessage ="Name:"+ This. Name +"\n\rage:"+ This. Age +"\r\nfriends:";  This. Friends.foreach (function (value, index, arr) {message+=" "+value;                    });                alert (message); }}} function Student (name, age, Job) {Person.call ( This, name, age);  This. Job =job; }        //It's inherited.Student.prototype =NewPerson (); Student.prototype.constructor=Student; varS1 =NewStudent ("Gck", -,"Sf"); S1.friends.push ("Core"); S1.        Showperson (); varS2 =NewStudent ("Carol", -,"Sf"); S2.    Showperson (); </script>

Combined inheritance avoids the defects of prototype chains and borrowing constructors, and blends their advantages into common inheritance patterns in JavaScript. Furthermore, instanceof and isprototypeof () can also be used to identify objects that are created based on composite inheritance.

  Note: Not ready to say the prototype chain and borrowing constructs, because these two flaws are too large, but there is a need to know to find out.

Parasitic combined inheritance

before It has been said that combinatorial inheritance is a common pattern of JavaScript inheritance, but it also has its own shortcomings. The big problem with the combination of inheritance is that in any case, the two-time superclass constructor is called: one time when you create a prototype of a subtype, and the other is inside the subtype constructor. Yes, the subtype will eventually contain all the instance properties of the superclass object, but we have to override those properties when we call the subtype constructor, so I inherit from the parasitic combination:

The so-called parasitic combined inheritance, that is, by borrowing the constructor to inherit the property, through the prototype chain of the hybrid form to inherit the method. The basic idea behind it is that you don't have to call a super-type constructor to specify a prototype of a subtype, all we need is a copy of the super-type prototype. Essentially, a parasitic inheritance is used to inherit a super-type prototype, and then the result is assigned to the child type's prototype. The basic pattern of parasitic combined inheritance is shown below.

<script>//If the Object.create browser is not supported, use this method instead        functionObject (o) {functionF () {} F.prototype=o; return NewF (); }        functionInheritprototype (subtype, supertype) {//Creating Objects            varPrototype = Object.create (Supertype.prototype);//This method ie9+ only supports            //var prototype = object (Supertype.prototype);            //Enhanced ObjectsPrototype.constructor =subtype; //Specifying ObjectsSubtype.prototype =prototype; }                functionPerson (name, age, friends) { This. Name =name;  This. Age =Age ;  This. Friends = ["Carol", "Seylia"]; if(typeof  This. Showperson! = "function") {                 This. Showperson =function () {                    varMessage = "Name:" + This. Name + "\n\rage:" + This. Age + "\r\nfriends:";  This. Friends.foreach (function(value, index, arr) {message+= " " +value;                    });                alert (message); }            }        }        functionStudent (name, age, Job) {Person.call ( This, name, age);  This. Job =job; }        //It's inherited.Inheritprototype (Student, person); varS1 =NewStudent ("Gck", +, "Sf"); S1.friends.push ("Core"); S1.        Showperson (); varS2 =NewStudent ("Carol", "Sf"); S2.    Showperson (); </script>

The efficient embodiment of this example is that it only calls the person constructor once, and therefore avoids the Student. Prototype create unnecessary and unnecessary attributes above. At the same time, the prototype chain remains intact, so instanceof and isprototypeof () can be used normally. developers generally believe that parasitic combined inheritance is an ideal inheritance paradigm for reference types.
YUI

"JavaScript review" inheritance

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.