[英]Is there a way to turn off the “this is not allowed before super” rule in babelify?
I'm running babelify 7.2.0 with Gulp and I'm getting an error on the following code: 我正在使用Gulp运行babelify 7.2.0并且我在以下代码中收到错误:
class One {}
class Two extends One {
constructor() {
this.name = 'John';
}
}
Here is the crux of the error: 以下是错误的关键:
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 |
It seems to me that this should not be firing because I am not making any super
calls within the constructor at all so there's no risk of conflict. 在我看来,这不应该被解雇,因为我根本没有在构造函数中进行任何
super
调用,因此没有冲突的风险。 I've submitted an issue already on Github but I'm wondering if there's a way I can turn it off in the mean time. 我已经在Github上提交了一个问题,但我想知道是否有办法可以在同一时间关闭它。
This isn't a bug. 这不是一个错误。 Subclasses must call
super
explicitly before attempting to access this
: 子类必须调用
super
明确地试图访问前this
:
class Two extends One {
constructor(...args) {
super(...args);
this.name = 'John';
}
}
This is defined in ECMAScript standard (see this answer), and Babel follows it closely. 这在ECMAScript标准中定义(参见本答案),Babel密切关注它。
No, there's no way to "turn this off" as this is a requirement defined in the ECMAScript 2015 standard. 不,没有办法“关闭它”,因为这是ECMAScript 2015标准中定义的要求。
this
is not available prior to super(...)
call in the constructor of an extending class. 在扩展类的构造函数中调用
super(...)
之前, this
是不可用的。
The exact details of where in the standard this is defined can be seen in this answer . 在这个答案中可以看到标准中定义的具体细节。
This isn't a Babel thing, it's defined in ES2015. 这不是巴别塔的事情,它在ES2015中定义。 You can't turn it off.
你不能把它关掉。
In a derived constructor, this
is undefined prior to calling super
. 在派生的构造函数中,在调用
super
之前, this
是未定义的。 Unlike (say) Java, calls to super
are never implicit, you must write them explicitly. 与(比如)Java不同,对
super
调用从不隐含,您必须明确地编写它们。
This is defined primarily in §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"). 这主要在§9.2.2 ,
[[Construct]] ( argumentsList, newTarget)
和§12.3.5.1中定义 , super
: [[Construct]]
的运行时语义只有在[[ConstructorKind]]
为“ [[Construct]]
时才调用OrdinaryCallBindThis
基数“(不是”派生“)。 Otherwise, this
remains undefined until you call super
, part of which calls [[Construct]]
on the super constructor, which either calls its own super
(if it is also a derived constructor) or OrdinaryCallBindThis
(if it's a base constructor). 否则,
this
仍然是未定义的,直到你调用super
,其中一部分在超级构造函数上调用[[Construct]]
,超级构造函数调用它自己的super
(如果它也是派生构造函数)或者OrdinaryCallBindThis
(如果它是基础构造函数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.