Why are object initializer methods not usable as constructors?

/#!/JoePea joe at trusktr.io
Wed Jul 27 02:49:32 UTC 2016

I feel like recent changes in the language (ES6+) introduce new features
that don't have the flexibility as the pre ES6 language that we're used to.

For example, [`super` is static and inflexible](
which is not inline with how `this` works.

Object initializer methods are also limited. Suppose I want to define an
object that contains various constructors. I would be inclined to use
object-initializer shortcuts:

let classes = {
  Cat() {},
  Dog() {},
  Bird() {}

but this doesn't work:

new classes.Dog() // Uncaught TypeError: classes.Dog is not a constructor

So, here's another failure of intuition (`super` being static was also not
intuitive). We can fix this by writing:

function Cat() {}
function Dog() {}
function Bird() {}

let classes = {

but that's not as convenient. What's the reason why we shouldn't be able to
do that? I feel like JavaScript is being restricted in undesirable ways. I
love JavaScript because it has always been so flexible, and I would expect
the new features to continue being flexible. That's what makes JavaScript

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160726/a93f7d89/attachment.html>

More information about the es-discuss mailing list