An update on rest operator ?

T.J. Crowder tj.crowder at farsightsoftware.com
Thu Aug 3 07:25:45 UTC 2017


On Thu, Aug 3, 2017 at 5:44 AM, James Browning
<thejamesernator at gmail.com> wrote:
>
> Given the above, it should really just be the case that
> `[...rest, last]` would just be the same as destructuring the
> reversed array, then re-reversing the rest part

Or the other way to think of it, since as you say it's going to end up in
an array anyway and by the time the expression is parsed, the number of
identifiers after the rest identifier is known:

```js
const [...rest, secondLast, last] = someIterable;
```

becomes

```js
const a = [...someIterable];
const rest = a.slice(0, -2);
const [secondLast, last] = a.slice(-2);
```

*(Theoretically; presumably the temporary arrays would be optimized out.)*

> Really the only contentious case is if it's in the middle because
> then you have to decide which direction to consume from e.g.
>
> ```
> const [a, ...rest, c] = [1]
>
> // Possible values for a, c
> // 1, 1
> // 1, undefined
> // undefined, 1
> ```

I understand the last two, where presumably `rest` is `[]` (but would
strongly argue for `1`, `[]`, `undefined` -- e.g., greediness), but what's
the logic that would explain `a = 1`, `c = 1`? That doesn't seem to make
any sense.

-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170803/831fd565/attachment.html>


More information about the es-discuss mailing list