An update on rest operator ?

Andy Earnshaw andyearnshaw at gmail.com
Thu Aug 3 09:29:26 UTC 2017

```I think T.J. had the most intuitive logic (and this has been mentioned in
previous threads too), where non-rest parameters have priority:

```
const [a, ...rest, c] = [1] // -> 1, [], undefined
const [a, ...rest, c] = [1, 2] // -> 1, [], 2
const [a, ...rest, c] = [1, 2, 3] // -> 1, [2], 3
```

If you think of rest as "everything else" (which is what it already is)
then this feels pretty natural and is easy to reason about.

On Thu, 3 Aug 2017 at 10:18 James Browning <thejamesernator at gmail.com>
wrote:

> The 1, 1 would happen if you decided that `[a, ...rest, b]` read in both
> directions (although personally I'm not a fan of this approach) e.g.
>
> ```
> const arr = [1]
> const [a, ...rest, b] = arr
>
> // Roughly equivalent to:
> const [a] = arr.slice(0, 1)
> const [c] = arr.slice(-1) // So they get duplicated
> const rest = arr.slice(1, -1) // Empty
>
> // ---- Similarly for a longer array
>
> const arr = [1, 2]
> const [a, b, ...rest, c, d] = arr
>
> // Would be roughly equivalent to
>
> const [a,b] = arr.slice(0, 2)
> const [d, c] = arr.slice(-2).reverse()
> const rest = arr.slice(2, -2) // Which is empty in this case
> ```
>
> ---
>
> One option could be (although I don't like it either) to allow the rest
> operator to have a direction e.g.:
>
> ```
> const [a, ...rest, b] = [1] // a -> 1, b -> undefined
> // And the other way
> const [a, rest..., b] = [1] // a -> undefined, b -> 1
> ```
>
> Personally I think that'd make it more confusing, but it's potentially an
> option.
>
> Another option could even be that `[a, ...rest, b]` simply throws on an
> iterable with less than 2 items, but that's not consistent with the current
> behavior of `[a, b]` not throwing on iterables with less than 2 items.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170803/4ccbb688/attachment.html>
```