如何实现B继承A

2018-03-29  本文已影响0人  茂茂爱吃鱼

法一:

B.prototype = new A();
B.prototype.constructor = B;

相当于

B: { prototype: { constructor: B }, __proto__: { __proto__: A.prototype: { __proto__: ... } } }

法二:

B.prototype = Object.create(A.prototype);

Object.create(o)相当于内部创建一个新的函数,然后将这个函数的prototype置为o,然后返回由这个函数创建的实例
那么整个过程相当于

function F() {}
F.prototype = A.prototype;
B.prototype = new F();
B: { prototype: { constructor: B }, __proto__: { __proto__: F.prototype[A.prototype] : { __proto__: ... } } } 
上一篇 下一篇

猜你喜欢

热点阅读