05:36
<rkirsling>
wait I don't think I realized that it's legal to return random crap from a constructor
05:37
<rkirsling>
so class C { foo() { return 3; } constructor() { return [1]; } } is an uninstantiable class then?
(in the sense that you can call new C() but you can never get "a C", so foo could only be accessed via C.prototype)
05:37
<rkirsling>
does that have a non-illegitimate usage?
15:15
<Aapo Alasuutari>
so class C { foo() { return 3; } constructor() { return [1]; } } is an uninstantiable class then?
(in the sense that you can call new C() but you can never get "a C", so foo could only be accessed via C.prototype)
Returning an object is valid.
15:16
<rkirsling>
it is valid code, yes, otherwise I wouldn't be talking about it lol
15:19
<bakkot>
depends on what you mean by "illegitimate". i believe the functionality is there so you could match es5-style "classes" which did the same thing. also lets you wrap the return value in a Proxy or whatever
15:20
<bakkot>
these days the only real use I see for it is to stamp private fields on existing objects (class id { constructor(x){ return x } }; class stamp { #priv; constructor(o){ super(o) } }; new stamp(foo); // foo now has .#priv
15:20
<bakkot>
the "return override trick"
15:20
<bakkot>
but this is hateful and you shouldn't do it
15:41
<shu>
loathsome return override
16:01
<rkirsling>
yeah by legitimate I meant not hateful/loathsome 😅 thanks for confirming
16:01
<rkirsling>
(I learned this from Shu's Shared Structs deck)
16:17
<Michael Ficarra>
I think it was used for like "decorating" a constructor back in the day
16:17
<Michael Ficarra>
something like C = doParameterValidation(C);
16:20
<Michael Ficarra>
but while that is a use case for return override in functions, it is not a use case for return override in class constructors
16:20
<Michael Ficarra>
that feature probably should've been left on the ES2015 cutting room floor
19:03
<Bradford Smith>
A factory function is better, but I have seen the "return from constructor" feature used to allow new Foo(x) to always return the same object for a given x value.
19:04
<shu>
that's high-level hating
19:04
<shu>
why would you want new to not be actually new