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:
- new Child() calls ChildConstructor.[[Construct]]
kind
is derived (earlier set in 14.5.15), so no newthis
value is bound- OrdinaryCallEvaluateBody is called, which ends up evaluating the body of the
Child
class constructor method, wheresuper()
is the first statement - super() calls ParentConstructor.[[Construct]], which takes us back to step 3, just with
kind
as base this time - Since
kind
is now base, a newthis
binding is created and bound to the Environment Record of the new function environment created for theParent
constructor - The rest of
Parent
constructor's body executes, and once done control flows back toChild.[[Construct]]
where execution continues from step 11 - Finally,
Child.[[Construct]]
tries to returnenvRec.GetThisBinding
Question
The Environment Record for the Child
constructor, created in step 6
of Child.[[Construct]]
, has no this
binding ([[ThisBindingStatus]]
is uninitialized). 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).
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.