An update on rest operator ?

James Browning thejamesernator at gmail.com
Thu Aug 3 09:18:45 UTC 2017


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170803/cb711887/attachment.html>


More information about the es-discuss mailing list