Iterating default function arguments

# Robin Cafolla (10 years ago)

I was wondering if there were any plans to modify arguments to include default parameters (e.g. changing it from a simpleParameterList) or to include a new property that does allow iteration of all values available to a function.

for example:

function foo( a, b = 2 ) {
    return arguments;
}

console.log( foo( 1 ) ); // outputs [ 1 ], not [ 1, 2 ]

Currently I can't see a way of getting default parameters as an iterable object.

I filed a bug with Mozilla over this, but they pointed out that the behaviour matches the spec.

bugzilla.mozilla.org/show_bug.cgi?id=1144672

# Rick Waldron (10 years ago)

This is correct, because only one argument was passed, therefore the arguments object has only one entry. Parameters are not the same as Arguments.

Therefore:

foo(1, 3) => [1, 3]

Because two arguments were passed. And:

foo(1, 2, 3) => [1, 2, 3]

Because three were passed.

Hopefully that helps clarify?

# Andrea Giammarchi (10 years ago)

I think the main concern was:

Currently I can't see a way of getting default parameters as an iterable object.

and indeed arguments is not the magic variable you are looking for.

However, you are inside your own defined function so either you don't specify defaults, or you iterate over [a, b] ?

Interesting enough, there's no actually a way to understand the function signature, in terms of optionally accepted arguments.

arguments.length is one and same is for foo.length so if we'd like to understand if anyone invoked the function passing or not all optional parameters, how could we proceed?

Lets say foo would like to delegate its own arguments including defined defaults to another function ... how can we grab these at runtime?

I think the answer is still a static [a, b] as list of arguments to apply, but I can see some lack of "reflection" capability ... or maybe Reflect would solve this?

# Brendan Eich (10 years ago)

Andrea Giammarchi wrote:

I think the answer is still a static [a, b] as list of arguments to apply, but I can see some lack of "reflection" capability ... or maybe Reflect would solve this?

Yes, Reflect -- we want a mirror approach, not more magic object like arguments or function.

# Andrea Giammarchi (10 years ago)

Sounds good to me and makes perfect sense. Robin?

# Robin Cafolla (10 years ago)

I'm all for using Reflect, but looking at the API as listed on MDN, the harmony-reflect github, or the spec, i don't see an obvious way of getting the parameters.

tvcutsem/harmony-reflect/blob/master/doc/api.md

What am I missing?

# Axel Rauschmayer (10 years ago)

I'm all for using Reflect, but looking at the API as listed on MDN, the harmony-reflect github, or the spec, i don't see an obvious way of getting the parameters.

If I understand you correctly (and this is not about parsing the parameter definitions) then how about the following solution?

function foo(...args) {
    let [a, b = 2] = args;
    return args;
}

Original code:

function foo( a, b = 2 ) {
    return arguments;
}
# Robin Cafolla (10 years ago)

Well my use case is for something I can insert into an existing function, in fact, a lot of functions, hopefully without manually listing the arguments.

function bar( parameters ) {
    for ( var i = 0; i < parameters.length; i++ ) {
        // do something interesting with each parameter of the function
    }
}

function foo( a, b = 2 ) {
    bar( parameters );

    // do normal foo stuff
}

Thinking about it I can't see how Reflect could do this, because it's surely meant to be external to the function. For example with an imaginary Reflect.getParameters:

Reflect.getParameters( foo ) // could only give me [ a: undefined, b: 2 ] as reflect can't know what foo was called with from outside the function.