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);
};