Good-bye constructor functions?

Benoit Marchant marchant at mac.com
Sun Dec 30 13:16:27 PST 2012


I'll take that occasion to mention that I feel ES doesn't need class at all.

We've been using Object.create in a way that allow us to create objects as prototypes for others, on these we add property descriptors, and then make "instances" off these prototypes without adding anything.

We use a create then init and never bother using a constructor function: pure, elegant, prototype inheritance.

This pattern works really well and is very natural for people with a class background.

Just saying...

Benoit

On Dec 30, 2012, at 11:47, Herby Vojčík <herby at mailbox.sk> wrote:

> Hello,
> 
> thinking about class / new / super semantics and its problems, I came with an idea. Big change on the first look, but in fact, imho, not so big and making cleaner model.
> 
> 
> 
> tl;dr
> ----
> 
> Constructor function were workaround to specify constructor for a class without `class`. Now, having `class` nothing presribes the class being equivalent to its constructor. It always brought confusion and now with `constructor` empowered, it brings two-space problem. The `class` keyword can return plain objects, which have `[[Construct]]` calling the constructor of the class.
> 
> 
> 
> The rest is in https://gist.github.com/4413009. If you could please read it, it tries to explain and go a little into detail. I feel this is needed to do now, putting constraints away later would be harder, as I write below:
> 
> 
> 
> Conclusion
> ---
> 
> If `class` would decouple the value it produces (the class) from the constructor function, it would create cleaner and more future-friendly model of classes. Coupling class and its constructor function was a necessity in the past, but now they need not to be coupled. Class.prototype.constructor takes all the responsibility for initializing the instance. This change involves little risk (only pieces of code who actually [[Call]] the "class" for manually initializing it; super-constructor call can be replaced by `super` or `Superclass.prototype.constructor.{call,apply}`). The big spec pieces that involve creation of classes and their instances are already very near and the important pieces need just a few changes. The devil is in the details, the spec is still very 'constructor must be a function'-oriented, but this is mainly in textual parts, algorithms are already generic enough. One important reason to ponder `class` not being constructor function is not conserving this tight coupling which in world of ES6+ with `class` can be seen as antipattern; freeing the contraints now is much easier than doing it later, when this unnecessary coupling is repeated in `class` semantic.
> 
> 
> Thanks, Herby
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list