Using `super` within an arrow function within an arrow function within a method

后端 未结 1 1758
清歌不尽
清歌不尽 2020-12-29 23:38

I\'m trying to figure out whether some behavior I\'m seeing in Node v4.1.1 (V8 v4.5.103.33) regarding super and arrow functions is specified behavior,

相关标签:
1条回答
  • 2020-12-29 23:45

    It appears that this is indeed a bug in V8 (it has now been fixed). Note that if there isn't the nested arrow function, it works fine.

    So if we're going to take a look through the literal specification text to see whether this is a bug, let's start with the super keyword itself:

    12.3.5.3 Runtime Semantics: MakeSuperPropertyReference(propertyKey, strict)

    The abstract operation MakeSuperPropertyReference with arguments propertyKey and strict performs the following steps:

    1. Let env be GetThisEnvironment( ).
    2. If env.HasSuperBinding() is false, throw a ReferenceError exception.
    3. Let actualThis be env.GetThisBinding().
    4. ReturnIfAbrupt(actualThis).
    5. Let baseValue be env.GetSuperBase().
    6. Let bv be RequireObjectCoercible(baseValue).
    7. ReturnIfAbrupt(bv).
    8. Return a value of type Reference that is a Super Reference whose base value is bv, whose referenced name is propertyKey, whose thisValue is actualThis, and whose strict reference flag is strict.

    Let's ignore most of the wordy stuff and worry about GetThisEnvironment():

    8.3.2 GetThisEnvironment ( )

    The abstract operation GetThisEnvironment finds the Environment Record that currently supplies the binding of the keyword this. GetThisEnvironment performs the following steps:

    1. Let lex be the running execution context’s LexicalEnvironment.
    2. Repeat
      a. Let envRec be lex’s EnvironmentRecord.
      b. Let exists be envRec.HasThisBinding().
      c. If exists is true, return envRec.
      d. Let outer be the value of lex’s outer environment reference.
      e. Let lex be outer.

    NOTE The loop in step 2 will always terminate because the list of environments always ends with the global environment which has a this binding.

    Now as we know that arrow functions don't have bindings to this, it should skip the environment record of the current function and the function immediately enclosing it.

    This will stop once reaching the "regular" functions and go on to retrieve the reference to the super object as expected, according to the specification.

    Allen Wirfs-Brock, project editor of the ECMAScript specification, seems to confirm this was intended in a reply on the es-discuss mailing list a few years back:

    super is lexically scoped, just like this to the closest enclosing function that defines it. All function definition forms except for arrow functions introduce new this/super bindings so we can just [say] that this/super binds according to the closest enclosing non-arrow function definition.

    0 讨论(0)
提交回复
热议问题