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,
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:
- Let env be GetThisEnvironment( ).
- If env.HasSuperBinding() is false, throw a ReferenceError exception.
- Let actualThis be env.GetThisBinding().
- ReturnIfAbrupt(actualThis).
- Let baseValue be env.GetSuperBase().
- Let bv be RequireObjectCoercible(baseValue).
- ReturnIfAbrupt(bv).
- 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:
- Let lex be the running execution context’s LexicalEnvironment.
- 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 likethis
to the closest enclosing function that defines it. All function definition forms except for arrow functions introduce newthis
/super
bindings so we can just [say] thatthis
/super
binds according to the closest enclosing non-arrow function definition.