[英]Understanding JavaScript super() invocations by following the ECMAScript specification
class Parent {
constructor() {}
}
class Child extends Parent {
constructor() {
super();
}
}
As I was trying to understand exactly how super()
invocations in class constructors work, I followed the following sequence of ECMAScript operations: 当我试图确切了解类构造函数中的
super()
调用如何工作时,我遵循以下ECMAScript操作序列:
kind
is derived (earlier set in 14.5.15 ), so no new this
value is bound kind
是派生的 (在14.5.15中先前设置),因此没有新的this
值绑定 Child
class constructor method, where super()
is the first statement Child
类构造方法的主体,其中super()
是第一条语句 kind
as base this time kind
为基础 kind
is now base , a new this
binding is created and bound to the Environment Record of the new function environment created for the Parent
constructor kind
现在是基础 , this
将创建一个新的this
绑定,并将其绑定到为Parent
构造函数创建的新函数环境的Environment Record Parent
constructor's body executes, and once done control flows back to Child.[[Construct]]
where execution continues from step 11 Parent
构造函数的其余主体执行,一旦完成,控制权流回到Child.[[Construct]]
,从第11步继续执行 Child.[[Construct]]
tries to return envRec.GetThisBinding
Child.[[Construct]]
尝试返回envRec.GetThisBinding
The Environment Record for the Child
constructor, created in step 6
of Child.[[Construct]]
, has no this
binding ( [[ThisBindingStatus]]
is uninitialized ). 在
Child.[[Construct]]
step 6
创建的Child
构造函数的环境记录没有this
绑定( [[ThisBindingStatus]]
未初始化 )。 Thus, when we try to do envRec.GetThisBinding
in step 8, we should as far as I can tell, get a ReferenceError
( as specified here ). 因此,当我们尝试在步骤8中执行
envRec.GetThisBinding
时,据我所知,应该获得ReferenceError
( 在此指定 )。
What am I missing here? 我在这里想念什么? I can't seem to see why the
Child
constructor won't throw an error, and indeed if the [[ThisValue]]
of the Child
constructor is set at all. 我似乎看不到为什么
Child
构造函数不会引发错误,而且确实没有设置Child
构造函数的[[ThisValue]]
。
You missed a step in your super()
link: 您错过了
super()
链接中的步骤:
- Let
newTarget
beGetNewTarget()
.让
newTarget
为GetNewTarget()
。- Assert:
Type(newTarget)
is Object.断言:
Type(newTarget)
是Object。- Let
func
be ?让
func
成为?GetSuperConstructor()
.GetSuperConstructor()
。- Let
argList
beArgumentListEvaluation
ofArguments
.让
argList
是ArgumentListEvaluation
的Arguments
。ReturnIfAbrupt(argList)
.ReturnIfAbrupt(argList)
。- Let
result
be ?让
result
成为?Construct(func, argList, newTarget)
.Construct(func, argList, newTarget)
。- Let
thisER
beGetThisEnvironment( )
.让
thisER
为GetThisEnvironment( )
。- Return ?
返回?
thisER.BindThisValue(result)
.thisER.BindThisValue(result)
。
Step 6
calls ParentConstructor.[[Construct]]
as you mentioned, but then step 8 will set the this
binding in the Child
constructor body to the constructed value, so when envRec.GetThisBinding
runs at the end off ChildConstructor.[[Construct]]
, the this
binding will be there 如前所述,第
6
步调用ParentConstructor.[[Construct]]
,但是第8步会将Child
构造函数主体中的this
绑定设置为构造后的值,因此,当envRec.GetThisBinding
在ChildConstructor.[[Construct]]
envRec.GetThisBinding
运行时ChildConstructor.[[Construct]]
, this
绑定将在那里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.