以下为JS寄生组合继承的实现方式 function Super(b){
this.b = b;
this.fun = function(){}
}
Super.prototype.c = function(){console.log(1111)}
function Foo(a,b){
this.a = a;
Super.call(this,b);
}
var f = new Function();
f.prototype = Super.prototype;
Foo.prototype = new f();
var foo1 = new Foo(1,2);为什么不直接用以下方式,更简洁而且能实现同样的效果 function Super(b){
this.b = b;
this.fun = function(){}
}
Super.prototype.c = function(){console.log(1111)}
function Foo(a,b){
this.a = a;
Super.call(this,b);
}
Foo.prototype = Super.prototype;
var foo1 = new Foo(1,2);
2 回答
月关宝盒
TA贡献1772条经验 获得超5个赞
你是不是打错了
Foo.prototype = Foo.prototype; Foo.prototype = Super.prototype;
Foo.prototype = Super.prototype;
Foo.prototype.c = function(){console.log(1111)}
和
Foo.prototype = new f();
还是不一样吧
添加回答
举报
0/150
提交
取消