【问题标题】:babeljs class static in constructor构造函数中的 babeljs 类静态
【发布时间】:2016-08-25 21:36:00
【问题描述】:

问题是如何在 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 没有像以前那样工作。你应该报告 new.target 不再被转译,暂时使用旧的 Babel 版本。

标签: javascript babel-6


【解决方案1】:

这似乎是 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"],
...

【讨论】:

    猜你喜欢
    • 2011-10-07
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多