JS Implementation inheritance

Source: Internet
Author: User

First, the prototype inheritance
            functionFather (lastname,sex) { This. lastname=lastname;//Surname                 This. sex=sex;//Sex            }            //Parent class prototype Propertiesfather.prototype.xo= ' xxx '; //sub-class            functionSon (firstname) { This. firstname=firstname;//name} son.prototype=NewFather (' Yang ');//prototype inheritance, son's prototype has pointed to an instance of the Father object                        varson =NewSon (' Ching '); alert (son.firstname);//Qingalert (son.lastname);//properties of Yang inheritancealert (SON.XO);//the stereotype attribute of xxx inheritancealert (son.sex);//undefined//value not passed when inheriting

Disadvantage: After inheriting must also give the parent class to pass the parameter, new Father (' Yang '), cannot pass the parameter directly into subclass

Second, borrowing constructor inheritance
            //Parent Class            functionFather (lastname,sex) { This. lastname=lastname;//Surname                 This. sex=sex;//Sex            }            //Parent class prototype Propertiesfather.prototype.xo= ' xxx '; //sub-class            functionSon (lastname,firstname) {Father.call ( This, LastName);//Bind the Father constructor to son                 This. firstname=firstname;//name            }                            varson =NewSon (' Yang ', ' Qing '); alert (son.firstname);//Qingalert (son.lastname);//properties of Yang inheritancealert (SON.XO);//undefined failed to inherit to prototype propertiesalert (son.sex);//undefined//does not require inheritance properties

Disadvantage: Failed to inherit to prototype property

Iii. combination Inheritance (prototype inheritance + borrowing constructor inheritance)
            functionFather (lastname,sex) { This. lastname=lastname;//Surname                 This. sex=sex;//Sex            }            //Parent class prototype Propertiesfather.prototype.xo= ' xxx '; //sub-class            functionSon (lastname,firstname) {Father.call ( This, LastName);//borrowing constructor Inheritance                 This. firstname=firstname;//name} son.prototype=NewFather ();//prototype inheritance, without argument only for inheriting prototype properties            varson =NewSon (' Yang ', ' Qing '); alert (son.firstname);//Qingalert (son.lastname);//properties of Yang inheritancealert (SON.XO);//XXX Inheritance to prototype propertiesalert (son.sex);//undefined//does not require inheritance properties

JS Implementation 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.