repeated parameter names and default values

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Sep 27 10:12:25 PDT 2012


Another:

The notes from last week's meeting said WRT function parameter bindings:

Conclusion/Resolution
- `var` bindings and are in scope within the function
- cannot use `let` to shadow a parameter
- defaults can refer to any top level binding

Using the above guidance, consider this one:

function test1(b=g) [
    function g() {};
    return typeof g +" "+ typeof b;
}
console.log(test1());   

presumably, this produces: "function function"
Which means that g is instantiated and initialized prior to apply the defaults to the parameters.

Now consider:

function test2(g,b=g) [
    function g() {};
    return typeof g +" "+ typeof b;
}
console.log(test2());   

By the same logic, this presumably must also produce: "function function"

So how about:

console.log(test2(1));   

Is it: "function number" or "function function" or "number number"

In ES5, 

function test3(g) [
    function g() {};
    return typeof g;
}
console.log(test3(1));   

produces "function", so the "number number" alternative doesn't seem plausible.  In EX5, nested function declarations over-write parameters that share the same name. 

"function number" may seem reasonable for  the second call of test2 but that seems inconsistent with the first call and with test1.

So, it seems that nested function declarations must be instantiated and bound before parameter instantiation/default value initialization and that parameters that share a name with a such function declaration do not get reinitialized (with either an argument or default value) during parameter instantiation.

Allen





More information about the es-discuss mailing list