Rest parameter syntax

Rick Waldron waldron.rick at gmail.com
Tue Jun 12 12:11:50 PDT 2012


On Tue, Jun 12, 2012 at 2:56 PM, Felix Böhm <esdiscuss at feedic.com> wrote:

> Another nice place for this syntax would be destructuring: If you want to
> get the last elements of an array, you might want to simply use
>
> [...arr, foo, bar] = arr;
>
>
> I really like that syntax. And in the end, that's what
>
> function(...arr, foo, bar){…}
>
> is doing. The difference to
>
> bar = arr.pop();
> foo = arr.pop();
>
> is that foo is preferred when only one value is available. Written in the
> JS of today, you'll need to write
>
> if(arr.length > 0){
>   if(arr.length !== 1){
>     bar = arr.pop();
>   }
>   foo = arr.pop();
> }
>
> And it'll get more complicated with every added variable.
>
> It's quite interesting that Herby used that syntax with the only feedback
> being that he can't use destructuring at that place :D
>
> @Rick: I don't get your point. Of course, undefined should be treated as
> any other value, everything else would be confusing. Or what were
> you referring to?
>

Revisit...

function foo( a, b, ...others, c ) {
  return [ a, b, others, c ];
}

foo( 1, 2, 3, 4, 5, 6, 7, 8, 9 );


It's clear to me that you want any formal params that follow a ...rest to
pop values off the arguments list until the params are satisfied, ie. c = 9.

The context of my question assumed there was a desire to avoid the
complexity and that the discussion had already reached consensus on only
allowing rest params at the end of a formal parameter list.

Rick




>
> As I've already written, parameters after rest can't have default values,
> which (partially) fixes the issue of optional parameters.
>
> 2012/6/12 T.J. Crowder <tj at crowdersoftware.com>
>
>> On 12 June 2012 18:14, Brendan Eich <brendan at mozilla.org> wrote:
>>
>>> Red guy still on your shoulder :-P.
>>
>>
>> I think you're seeing things. ;-) But seriously, I don't really have a
>> horse in this race (at all, I was just exploring the concept -- seems
>> that's already been done), happy to leave it at that.
>>
>> -- T.J.
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> 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/20120612/43be1ef6/attachment.html>


More information about the es-discuss mailing list