rest parameters
Tab Atkins Jr.
jackalmage at gmail.com
Fri Oct 2 23:27:32 UTC 2015
On Fri, Oct 2, 2015 at 3:00 PM, Michaël Rouges <michael.rouges at gmail.com> wrote:
> For the undefined case, it isn't unexistent...
>
> ```JavaScript
> void function () {
> console.log(arguments.length); // 3
> }(1, 2, undefined);
> ```
>
> Then, I don't see how it may be ambiguous...
Again, this has been discussed in the past. It would be useful for
you to look it up in the archives and find the previous discussion.
A few examples of the ambiguity:
function f1(...a, b, c) {print arguments}
f1(1) // ([], 1, undefined) or ([], undefined, 1) ?
function f2(...a, b="default") {print arguments}
f2(1) // ([], 1) or ([1], "default") ?
function f3(a, b, ...c, d, e) {print arguments)
f3(1,2,3) // (1, 2, [], 3, undefined) or (1, 2, [], undefined, 3) or
(1, undefined, [], 2, 3) or something else?
With the rest param limited to being at the end, it's clear how to
answer questions similar to these - you just assign arguments
normally, and then if there are any left over, they get packed up in
the rest param.
When arguments can follow the rest param, it's not clear how to do it.
You can't just ignore the rest param, assign args, and then take the
leftovers, because the rest param needs to take from the middle.
You're clearly grabbing from "each end" and then if there's any left
in the middle, you pack them into the rest param, but how to do so?
That's the essence of the f1 and f3 ambiguities. And optional
arguments (f2) are similarly difficult - should they "grab" more
strongly than the rest param?
You can come up with answers to these questions. What you can't do is
come up with answers that are *obviously correct*. This is why I'm
not aware of any language that allows this syntax.
~TJ
More information about the es-discuss
mailing list