Proposal for faster this assignments in constructor functions
dante federici
c.dante.federici at gmail.com
Fri Dec 7 13:56:26 UTC 2018
I can see that, although I'm struggling to come to that conclusion if I saw
that block of code fresh. I don't think it would be immediately clear,
though. In your interpretation of "this has 5 arguments", what would you
expect them to be? `constructor(x, y, a, b, c)`? Just prepend them?
I'm trying to work with the idea of the existing constructor function and a
possible shorthand. Another problem with almost any suggestion is how it
may interact with destructuring and expectations (with any additions of
"bind these").
I am highly suspect that any solution that is both ergonomic and concise
could pass.
What about making an explicit `constructor` function invalid in the case of
the shorthand syntax?
```js
class Point(x, y) {
...
}
// As shorthand for:
class Point {
constructor(x, y){
this.x = x;
this.y = y;
}
}
```
If this kind of syntax can't fly, that's fine, but I feel there isn't going
to be an ergonomic way to have a simple way to bind constructor arguments.
Although that makes things like super calls potentially awkward, we can
also assume that if you extend a class that it calls super in the generated
constructor. I'm not offended by a short hand as my last post, as the
transition is quite natural if you need a more complex use case:
```js
class Point(x, y) {
toString() { ... }
}
// Oh no, now I have complex logic!
class Point {
constructor(x, y) {
Object.assign(this, {x, y});
...
}
toString() { ... }
}
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20181207/29526c9d/attachment.html>
More information about the es-discuss
mailing list