【发布时间】:2019-03-08 18:55:08
【问题描述】:
我想创建一个包装器机制:我们包装c,所以新对象w 有自己的属性和方法,但c 也可以访问。
// Note: this class might be from an external lib
class C {
f() {
console.log('f (original)');
this.p = 'p';
}
}
class W {
f() {
console.log('f (new)');
super.f(); // TypeError: (intermediate value).f is not a function
console.log(this.p);
}
}
// Note: this value is external for us
const c = new C();
const w = Object.create(null, Object.getOwnPropertyDescriptors(W.prototype));
Object.setPrototypeOf(w, c);
w.f(); // expected:
// f (new)
// f (original)
// p
我这样做是否正确?
为什么会发生错误?
更新:P.S.我确实知道我可以使用组合,但我想了解错误的来源。
【问题讨论】:
-
可能是 stackoverflow.com/questions/2107556/… 的副本 ...在那里阅读它,它可能会解决您的问题。
-
@gugateider 谢谢但抱歉,我没有看到答案
-
你想包装什么
obj?您发布的代码中没有obj变量。 -
更新:P.S.我确实知道我可以使用组合,但我想了解错误的来源。
标签: javascript oop inheritance ecmascript-6 super