Closed Bug 1144672 Opened 7 years ago Closed 7 years ago

Function arguments don't include default parameters


(Core :: JavaScript Engine, defect)

36 Branch
Not set





(Reporter: robincafolla, Unassigned)



(1 file)

Attached file default-arguments.html
User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0
Build ID: 20150224134236

Steps to reproduce:

Tried to access the arguments of a function with a default parameter.

var f = function( a, b = 1 ) {
    return arguments;
f( 1 );

Actual results:

arguments doesn't include the default parameter, only parameters passed to the function are listed.

returns [ 1 ]

Expected results:

arguments should include default parameters.

should return [ 1, 1 ]
Ever confirmed: true
OK - I *think* what the ES6 specification says here is:

1.  Because the argument list contains a default parameter, an unmapped arguments object is
    created instead of the normal mapped kind.

    I am actually not entirely clear on this point, but note that CreateMappedArgumentsObject
    would flunk an assertion if we got there:

    > 1.  Assert: formals does not contain a rest parameter, any binding patterns, or any
    >     initializers. It may contain duplicate identifiers.

    "Initializers" means default parameters.

2.  Unmapped arguments objects contain the original values of the arguments actually passed.
    They're created before default arguments are populated and they don't get updated later
    when the default values are filled in.

I'm going to check with the spec editor and see what we can figure out.

Setting needinfo?me so I don't forget about this.
Flags: needinfo?(jorendorff)
OK, before checking with the spec editor I double-checked the spec one last time, and it says:
> Static Semantics: IsSimpleParameterList
> [...]
> SingleNameBinding : BindingIdentifier Initializer
> 1.  Return false.

That is, a parameter with an Initializer, like `b = 1`, is *not* a simple parameter. So this function gets an unmapped arguments object containing only the arguments actually passed in. (phew!)

One reason for this is so you can use arguments.length to ask how many arguments were actually passed.

Rest parameters are a nicer alternative to the `arguments` object in most situations.
Closed: 7 years ago
Resolution: --- → INVALID
Flags: needinfo?(jorendorff)
So that means that there's no way to iterate over the default arguments of a function? I'd consulted the spec but it didn't seem clear to me what should be happening.
You need to log in before you can comment on or make changes to this bug.