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