`super` and superclass constructor return values
# Eric Faust (9 years ago)
My understanding is that this should log "true". Class constructors may return any object they wish, and needn't return instances of the class.
Hopefully, this will allow you to simplify.
Thanks,
# Logan Smyth (9 years ago)
Yup, correct. The value of the this
binding is initialized to the result
of [[Construct]]
, which will either be the parent constructor this
binding, or the explicitly returned value.
Relevant spec links:
Evaluating super()
: (12.3.5.1)
www.ecma-international.org/ecma-262/6.0/#sec-super-keyword-runtime-semantics-evaluation
Returning a value from a constructor: (9.2.2 step 13.b)
www.ecma-international.org/ecma-262/6.0/#sec
What should
this
be in classB
? The value ofobject
or an instance ofB
? It's an edge case, but could simplify my code a little if it's the former.const object = {} class A { constructor() { return object } } class B extends A { constructor() { super() console.log(this === object) } }