问题是如何在调用super方法之前从构造函数内访问类静态属性?
class A
{
constructor(input) {
console.log('A', typeof new.target);
}
}
class B extends A
{
static template = '';
constructor() {
console.log('B', typeof new.target);
super();
}
}
class C extends B
{
static template = 'CCC';
}
new C();
出于某种原因我得到了:
B undefined
A undefined
而不是
B function
A function
我已经already asked this question大约一年前。目前,其中提供的解决方案不再可行。
您可以尝试babel console中的代码。有趣的是,这段代码在没有 babel 的情况下(例如在最新的 Chrome 中)并且当 es2015 复选框关闭时也能正常工作。
最佳答案
这似乎是 babel 中的一个错误。我编写了一个转换 new.target 的小插件:
https://github.com/vbarbarosh/babel_plugin_transform_es2015_newtarget
$ npm install --save-dev git://github.com/vbarbarosh/babel_plugin_transform_es2015_newtarget
$ cat .babelrc
...
"plugins": ["transform-es2015-newtarget"],
...
关于javascript - babeljs 类在构造函数中静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39155082/