JavaScript Parasitic Compositional Inheritance

function inheritPrototype(subType, superType){
 var prototype = Object.create(superType.prototype); //Create an object
 prototype.constructor = subType; //Enhanced object
 subType.prototype = prototype; //Specify the object
}

 The basic pattern of parasitic composition inheritance is as above.

function SuperType(name){
 this.name = name;
 this.colors = ["red", "blue", "green"];
}
SuperType.prototype.sayName = function(){
 alert(this.name);
};
function SubType(name, age){
 SuperType.call(this, name);

 this.age = age;
}
//  parasitic combination
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function(){
 alert(this.age);
};

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326174857&siteId=291194637