87,903
社区成员
发帖
与我相关
我的任务
分享
/*寄生组合式继承*/
function SuperType(name){
this.name=name;
this.colors=["blue","red","black"];
this.age=22;
}
SuperType.prototype.sayName=function(){
alert(this.name);
}
function SubType(name,age){
SuperType.call(this,name);
this.age=age;
}
function inheritPrototype(SuperType,SubType){
var prototype=Object(SuperType.prototype);
SubType.prototype.constructor=SubType;
SubType.prototype=prototype;
}
inheritPrototype(SuperType,SubType);
SubType.prototype.sayAge=function(){
alert(this.age);
}
for(var prop in SuperType.prototype){
alert(prop);
}
alert(SuperType.prototype==SubType.prototype);
function inheritPrototype(SuperType,SubType){
var prototype=Object(SuperType.prototype);
SubType.prototype.constructor=SubType;//这里的代码是否有误,个人觉得是否为prototype.constructor=SubType;?
SubType.prototype=prototype;
}