我正在使用 ES6 类,我的类 (A) 扩展了类 B,类 B 扩展了类 C。A 如何扩展方法,然后调用 C 的该方法版本。
class C {
constructor() {
console.log('class c');
}
}
class B extends C {
constructor() {
super()
console.log('no, I don't want this constructor.');
}
}
class A extends B {
constructor() {
// What should I be doing here? I want to call C's constructor.
super.super();
}
}
编辑:谢谢大家,我将停止尝试做这种愚蠢的事情。在我的情况下,代码重用的微小收获不值得花功夫。
最佳答案
You can’t not call the parent’s constructor in an ES6 class.根据您的评论判断,也许您应该尝试这样的事情?
class Mixin {
static include(constructor) {
const descriptors = Object.getOwnPropertyDescriptors(Mixin.prototype);
Object.keys(descriptors).forEach(name => {
Object.defineProperty(constructor.prototype, name, descriptors[name]);
});
}
someCommonFunction() {
// Perform operation common to B and C
}
}
delete Mixin.prototype.constructor;
class B extends A {
}
Mixin.include(B);
class C extends A {
}
Mixin.include(C);
关于javascript - 如何在 ES6 中调用类的父类的父类的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861855/