Below is a classic example for Parasitic Combination Inheritance. Can I rewrite inheritPrototype() as the following? Also, why need to set the "constructor" inside the function?
function inheritPrototype(subType, superType){
subType.prototype = superType.prototype; //assign object
}
Classic Example
function object(my_object){
function F(){}
F.prototype = my_object;
return new F();
}
function inheritPrototype(subType, superType){
var x = object(superType.prototype); //create object
x.constructor = subType; //augment object
subType.prototype = x; //assign object
}
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;
}
// ***
inheritPrototype(SubType, SuperType);
SubType.prototype.sayAge = function(){
alert(this.age);
};