Clarification on function default param values

Jeff Morrison lbljeffmo at gmail.com
Mon Sep 30 13:17:14 PDT 2013


So I think during the last meeting it was decided that we'd now have two 
scopes for functions with default param values: One for head/params, and 
one for the function body.
Was it also agreed that we'd use let-binding (vs var-binding) for the 
default-value expressions?

I also just wanted to clarify some of the awkward edge-case scenarios as 
I understand them to be sure we're all on the same page:

var x=1, y=2;
function foo(x=3, y=y, z=y) {
   return [x, y, z];
}
foo(); // [3, undefined, undefined] ? or [3, 2, 2] ?
foo(2); // [2, undefined, undefined];
foo(2, 3); // [2, u];
x; // 1
y; // 2


var x = 1, y=2;
function foo(x=3,y=x+1) {
   return [x, y];
}
foo(); // [3, 4];
foo(2); // [2, 4];
foo(2, 3); // [2, 3];
x; // 1
y; // 2


var x = 1, y=2;
function foo(x=3, y=(x=undefined,4)) {
   return [x,y];
}
foo(); // [undefined, 4]
foo(2); // [undefined, 4]
foo(2, 3); // [2, 3] ? or [undefined, 3] ?
x; // 1
y; // 2


function foo(bar=(function() { return 'param-inner'; })) {
   var ret = bar();
   function bar() {
     return 'body-inner';
   }
   return ret;
}
foo(); // 'body-inner'
foo(function() { return 'futility'; }); // 'body-inner'


More information about the es-discuss mailing list