On Wed, Nov 28, 2018 at 6:33 PM Simo Costa
<[email protected]> wrote:
>
> So my proposal is to avoid those repetitions...

I'm a bit surprised to find that no one's mentioned Bob Myers'
[proposal][1] for pick notation yet, which would readily address this
requirement *and* various other requirements beyond initializing
newly-constructed objects.

Using Bob's current draft syntax, Simo's example would be:

```js
constructor(par1, par2, par3) {
    this.{par1, par2, par3} = {par1, par2, par3};
}
```

or if the constructor accepts an object:

```js
constructor(options) {
    this.{par1, par2, par3} = options;
}
```

But I think we can go further if we tweak the syntax a bit. Perhaps:

```js
constructor(par1, par2, par3) {
    this.{} = {par1, par2, par3};
}
```

...where the property names are inferred from the properties on the
right-hand side. That would be functionally equivalent to:

```js
constructor(par1, par2, par3) {
    Object.assign(this, {par1, par2, par3});
}
```

...but since the syntax is clear about the intent, when an object
initializer (rather than just object reference) is used on the right-hand
side it's an optimization target if a constructor is "hot" enough to
justify it (e.g., an engine could optimize it into individual assignments).

For me, that would be a great, clear, concise feature, and hits the other
use cases Bob mentions in his proposal. I like that I'm still in control of
what parameters get assigned as properties (which I think has been true of
all of the suggestsions in this thread).

-- T.J. Crowder

[1]: https://github.com/rtm/js-pick-notation
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to