简体   繁体   English

通过遵循ECMAScript规范了解JavaScript super()调用

[英]Understanding JavaScript super() invocations by following the ECMAScript specification

Code

class Parent {
  constructor() {}
}

class Child extends Parent {
  constructor() {
    super();
  }
}

Background 背景

As I was trying to understand exactly how super() invocations in class constructors work, I followed the following sequence of ECMAScript operations: 当我试图确切了解类构造函数中的super()调用如何工作时,我遵循以下ECMAScript操作序列:

  1. new Child() calls ChildConstructor.[[Construct]] 新的Child()调用ChildConstructor。[[Construct]]
  2. kind is derived (earlier set in 14.5.15 ), so no new this value is bound kind派生的 (在14.5.15中先前设置),因此没有新的this值绑定
  3. OrdinaryCallEvaluateBody is called, which ends up evaluating the body of the Child class constructor method, where super() is the first statement 调用OrdinaryCallEvaluateBody ,最终评估Child类构造方法的主体,其中super()是第一条语句
  4. super() calls ParentConstructor.[[Construct]] , which takes us back to step 3 , just with kind as base this time super()调用ParentConstructor。[[Construct]] ,这使我们回到步骤3 ,这次只是以kind基础
  5. Since 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
  6. The rest of Parent constructor's body executes, and once done control flows back to Child.[[Construct]] where execution continues from step 11 Parent构造函数的其余主体执行,一旦完成,控制权流回到Child.[[Construct]] ,从第11步继续执行
  7. Finally, Child.[[Construct]] tries to return envRec.GetThisBinding 最后, Child.[[Construct]]尝试返回envRec.GetThisBinding

Question

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()链接中的步骤:

  1. Let newTarget be GetNewTarget() . newTargetGetNewTarget()
  2. Assert: Type(newTarget) is Object. 断言: Type(newTarget)是Object。
  3. Let func be ? func成为? GetSuperConstructor() . GetSuperConstructor()
  4. Let argList be ArgumentListEvaluation of Arguments . argListArgumentListEvaluationArguments
  5. ReturnIfAbrupt(argList) . ReturnIfAbrupt(argList)
  6. Let result be ? result成为? Construct(func, argList, newTarget) . Construct(func, argList, newTarget)
  7. Let thisER be GetThisEnvironment( ) . thisERGetThisEnvironment( )
  8. 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.GetThisBindingChildConstructor.[[Construct]] envRec.GetThisBinding运行时ChildConstructor.[[Construct]]this绑定将在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM