Rest parameter anywhere

C. Scott Ananian ecmascript at cscott.net
Fri Apr 11 17:09:25 PDT 2014


On Fri, Apr 11, 2014 at 1:53 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
> On Fri, Apr 11, 2014 at 5:39 AM, Егор Николаев <termi1uc1 at gmail.com> wrote:
>> 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
>> ```
>
> You skipped the most interesting case - what to do when the list is
> *almost* long enough to accommodate all of the non-rest params.
>
> `let [a = 1, b = 2, ...rest, c = 4, d = 5] = [9, 8, 7];`
>
> It seems, from your second test case, that a=9 and b=8, but which
> variables gets a value of 7 - c or d?

FWIW, in coffeescript `c` gets the 7:

http://coffeescript.org/#try:f%20%3D%20%28a%3D1%2Cb%3D2%2Cc...%2Cd%3D3%2Ce%3D4%29%20-%3E%20alert%20%22a%3D%23{a}%20b%3D%23{b}%20c%3D%23{c}%20d%3D%23{d}%20e%3D%23{e}%22%0Af%288%2C9%2C10%29

```coffee
f = (a=1,b=2,c...,d=3,e=4) -> alert "a=#{a} b=#{b} c=#{c} d=#{d} e=#{e}"
f(8,9,10)  // alerts "a=8, b=9, c= d=10 e=4"
```
  --scott


More information about the es-discuss mailing list