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](
https://esdiscuss.org/topic/the-super-keyword-doesnt-work-as-it-should)
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:
```js
let classes = {
Cat() {},
Dog() {},
Bird() {}
}
```
but this doesn't work:
```js
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:
```js
function Cat() {}
function Dog() {}
function Bird() {}
let classes = {
Cat,
Dog,
Bird
}
```
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
great.
*/#!/*JoePea
-------------- 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