Rest parameter anywhere

Егор Николаев termi1uc1 at gmail.com
Fri Apr 11 05:39:40 PDT 2014


Long story short:
```javascript
function test( a = 1, b = 2, ...rest, c = 4, d = 5) { console.log(a, b,
rest, c, d) }

test();                // 1, 2, [], 4, 5
test(9, 8);            // 9, 8, [], 4, 5
test(9, 8, 7, 6);      // 9, 8, [], 7, 6
test(9, 8, 7, 6, 5, 4);// 9, 8, [7, 6], 5, 4

let [a = 1, b = 2, ...rest, c = 4, d = 5] = [];                // a == 1, b
== 2, rest == [], c == 4, d == 5
let [a = 1, b = 2, ...rest, c = 4, d = 5] = [9, 8];            // a == 9, b
== 8, rest == [], c == 4, d == 5
let [a = 1, b = 2, ...rest, c = 4, d = 5] = [9, 8, 7, 6];      // a == 9, b
== 8, rest == [], c == 7, d == 6
let [a = 1, b = 2, ...rest, c = 4, d = 5] = [9, 8, 7, 6, 5, 4];// a == 9, b
== 8, rest == [7, 6], c == 5, d == 4
```

This is easy-to-implement but very important destructuring pattern. If it
can't be in ES6 maybe in ES7?

Issue: https://bugs.ecmascript.org/show_bug.cgi?id=2034
Previous discussion:
https://mail.mozilla.org/pipermail/es-discuss/2012-June/023393.html

CoffeeScript and Python 3 has this feature as well.

Examples:
```javascript
let [firstOne, ...rest, lastOne] = document.findAll(".someClass");
firstOne.classList.add("first");
lastOne.classList.add("last");


function download (...files, callback) {
    for (let file of files) {
      <...>
      if (downloaded) callback();
    }
}

download( 'file1.txt', 'file2.txt', function() {
    alert('ready');
});
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140411/daebd71d/attachment.html>


More information about the es-discuss mailing list