为了账号安全,请及时绑定邮箱和手机立即绑定

JS寄生组合继承的疑问

JS寄生组合继承的疑问

蓝山帝景 2018-07-23 14:22:34
以下为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();

还是不一样吧


查看完整回答
反对 回复 2018-08-02
  • 2 回答
  • 0 关注
  • 640 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号