Duplicate super call behaviour

Sebastian McKenzie sebmck at gmail.com
Sun Oct 25 00:00:24 UTC 2015


I was recently reading the specification on the behaviour of duplicate
super() calls in derived constructors. Reading the grammar the following is
valid:

```
class Foo {
  constructor() {
    console.log("foobar);
  }
}

class Bar extends Foo {
  constructor() {
    super();
    super();
  }
}

new Bar;
```

However my reading of the runtime semantics, it will actually execute the
constructor body *twice. *And so "foo" will be printed two times.

This is because the only thing I've found stopping duplicate super calls is
step 10 of SuperCall in 12.3.5.1
<http://www.ecma-international.org/ecma-262/6.0/index.html#sec-super-keyword-runtime-semantics-evaluation>
:

   1. Let newTarget be GetNewTarget().
   2. If newTarget is undefined, throw a ReferenceError exception.
   3. Let func be GetSuperConstructor().
   4. ReturnIfAbrupt(func).
   5. Let argList be ArgumentListEvaluation of Arguments.
   6. ReturnIfAbrupt(argList).
   7. Let result be Construct(func, argList, newTarget).
   8. ReturnIfAbrupt(result).
   9. Let thisER be GetThisEnvironment( ).
   10. Return thisER.BindThisValue(result).

Since BindThisValue will throw a ReferenceError if `this` has already been
initialised:


   1. Let envRec be the function Environment Record for which the method
   was invoked.
   2. Assert: envRec.[[thisBindingStatus]] is not "lexical".
   3. If envRec.[[thisBindingStatus]] is "initialized", throw a
   ReferenceError exception.

But this check is performed at step 10 whereas the super constructor is
actually evaluated at step 7.

Is my reading correct? If so, to me this behaviour seems quite unexpected.

Any insight (or correction) is extremely appreciated, thank you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151025/191c0632/attachment.html>


More information about the es-discuss mailing list