Good-bye constructor functions?

Herby Vojčík herby at mailbox.sk
Sun Dec 30 11:47:47 PST 2012


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


More information about the es-discuss mailing list