有没有办法关闭 babelify 中的 "this is not allowed before super" 规则?

Is there a way to turn off the "this is not allowed before super" rule in babelify?

我 运行 babelify 7.2.0 Gulp 我在以下代码中遇到错误:

class One {}

class Two extends One {
  constructor() {
    this.name = 'John';
  }
}

错误的症结所在:

SyntaxError: [the file path in question]: 'this' is not allowed before super()
  20 | class Two extends One {
  21 |   constructor() {
> 22 |     this.name = 'John';
     |     ^
  23 |   }
  24 | }
  25 | 

在我看来,这不应该触发,因为我根本没有在构造函数中进行任何 super 调用,因此没有冲突的风险。我已经在 Github 上提交了一个问题,但我想知道是否有办法同时将其关闭。

这不是错误。在尝试访问 this:

之前,子类 必须 调用 super 显式
class Two extends One {
    constructor(...args) {
        super(...args);
        this.name = 'John';
    }
}

这是在 ECMAScript 标准中定义的(参见 回答),Babel 紧随其后。

不,没有办法 "turn this off",因为这是 ECMAScript 2015 标准中定义的要求。

this 在扩展 class.

的构造函数中调用 super(...) 之前不可用

有关标准中定义此内容的确切详细信息,请参见

这不是 Babel 的东西,它是在 ES2015 中定义的。您无法将其关闭。

在派生构造函数中,this 在调用 super 之前未定义。与(比如)Java 不同,对 super 的调用从不隐含,您必须明确地编写它们。

这主要在 §9.2.2, [[Construct]] ( argumentsList, newTarget), and §12.3.5.1, the runtime semantics for super: [[Construct]] only calls OrdinaryCallBindThis if the [[ConstructorKind]] is "base" (not "derived"). Otherwise, this remains undefined until you call super 中定义,其中一部分在超级构造函数上调用 [[Construct]],超级构造函数调用它自己的 super(如果它也是派生构造函数)或OrdinaryCallBindThis(如果它是基础构造函数)。