JavaScript寄生组合式继承

function inheritPrototype(subType, superType){
 var prototype = Object.create(superType.prototype); //创建对象
 prototype.constructor = subType; //增强对象
 subType.prototype = prototype; //指定对象
} 

 寄生组合式继承的基本模式如上。

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

猜你喜欢

转载自wyf.iteye.com/blog/2408048
今日推荐