Accesssing ES6 class constructor function

James Treworgy jamietre at
Fri Jan 6 02:45:32 UTC 2017

Thanks, that's pretty interesting. Curiously, my use case isn't part of the
discussion - the proposals are focused on new syntax to deal with the
"99.9%" case of wanting a factory. Not really addressed is simply "people
may want to call it for some other reason", I guess I'm the 0.1% case.

It seem to me that allowing invocation through `` or
`function.apply` would be pretty reasonable. Using these mechanisms is very
explicit. You would not lose the protection gained by prohibiting normal
new-less calls, and it wouldn't require creating any new syntax to expose
the function.

"YK: lets go back to es5, a massive hazard to forget use. This made many
people not want the newless behavior."

This is fine (though not sure I agree it was such a massive hazard in
practice - linters have done a fine job of letting us know when we screwed
up for a long time) - but it seems that prohibiting invication through any
and all means is overkill - and has negative consequences for advanced use
cases and interop with ES5.

"MM: we should drop this completely, and solve it with decorators."

I'm not sure what that would look like but I think it only refers to
newless construction..

On Jan 5, 2017 8:52 PM, "Allen Wirfs-Brock" <allen at> wrote:

> Notes on the most recent TC39 discussion of callable class constructors is
> at
>  referring to this deck
> On Jan 5, 2017 9:31 AM, James Treworgy <jamietre at> wrote:
> Hi - I am brand new to this list, I find myself here because of a
> confounding issue related to ES6 classes vs. traditional constructors.
> Forgive me if this is something that's been hashed out in times past. I
> looked around for discussion online and couldn't find anything more than
> the observation that the spec prohibits invoking it - not really any
> discussion. Probably a failing of google more than anything else, so if
> there's some discussion that I should read to catch up please point me
> there.
> Here's my issue. The ES6 spec prohibits invoking class constructors
> without "new". This makes such functions a special case, e.g.
> class Test() {}
> // typeof Test === 'function'  // yep
> // Test.prototype.constructor === Test // yep
> // Test() => nope ... TypeError: Class constructor Test cannot be invoked
> without 'new'
> // ... nope
> // Test.apply() ... nope
> This has some interesting consequences. It means testing something for
> typeof "function" no longer guarantees it can be invoked without error.
> Also "function.toString()" can now return something that isn't actually a
> legal function definiton (since it returns the whole class as text). There
> seems to be no method, through various javascript reflection/invocation
> techniques or otherwise, to invoke a class constructor except by creating
> an instance of the class.
> For tool-builders the consequences of this are significant. It's no longer
> possible to create something that can extend/wrap/act on a prototype by
> intercepting it's construction process, as it was before with plain ES5
> constructors. So classes are fundamentally different than prototype
> contructors in how we can use them, far more than syntactic sugar. This has
> come into play lately for me, as an DI container we use that does exactly
> this doesn't work with ES6 classes (and as far as I can tell, there's no
> way to make it work, other than having devs no longer use class syntax).
> This seems a strange design decision. Even conventional OO languages like
> C# have the capability to reflect on classes and access the constructor
> directly as a function. It seems to fly in the face of the basic
> openness/dyanamic nature of JavaScript, and more signficantly, creates a
> kind of backward incompatibility since a function is no longer just a
> function.
> I'm wondering whether I'm missing some mechanism for legally accessing a
> class constructor as a function (other than parsing the output of
> toString() and eval!) -- and generally thoughts on this aspect of the ES6
> specification.
> Thank you!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list