Open nicolo-ribaudo opened 3 years ago
We were close to fixing this a few years ago: https://esdiscuss.org/topic/primitive-values-from-class-constructors https://github.com/tc39/ecma262/pull/469
Thanks for the links!
I see that #469 was closed because v8's metrics show that ~1.5% of the web relies on returning primitives from the constructor (https://github.com/tc39/ecma262/pull/469#issuecomment-383340969). However, I wonder if that data only includes new class { constructor() { return 1 } }
or also new function() { return 1 }
: I tried manually checking ~20 of the "Sample URLs" at https://www.chromestatus.com/metrics/feature/timeline/popularity/2054 relying on this behavior, and none of them was using the class
keyword :thinking:
@gsathya can you confirm whether the above metric differentiated between class
and function
?
Description:
Consider these two classes:
when instantiated, they have different behaviors depending on the value of
x
:x
new A()
new B()
x
x
undefined
thisObject
thisObject
thisObject
TypeError
I don't see any spec-reason to have different behaviors in the "base" and "derived" classes. I think both classes should throw, or (if throwing is web-incompatible) both should return
thisObject
.eshost Output:
All the engines (except for Hermes, which always throws a
SyntaxError
) conform to the specification.Both Babel and TypeScript return
thisObject
instead of throwing aTypeError
(I discovered this inconsistency while improving Babel's spec compliance).context/history
The step that throws the
TypeError
(12.c of [[Construct]]) has been in the spec since the creation ofspec.html
in this repository.I couldn't find anything on ESDiscuss except for https://esdiscuss.org/topic/should-the-default-constructor-return-the-return-value-of-super#content-24: it looks in an es2015 draft classes didn't return the result of
super()
by default.There is some discussion related to how
super()
initializes the defaultthis
at https://github.com/tc39/notes/blob/827c5f98554b6e04d81c2c265f78f6f7ddf8415a/meetings/2014-09/sept-24.md, and there was a mention by BE that when instantiating an es5 function, if it returns a primitive you always get back thethis
object.