Generator issue: exceptions while initializing arguments

Brendan Eich brendan at mozilla.com
Tue Sep 11 01:33:41 PDT 2012


Nothing is new here, under the straightforward semantics Jason 
described. JS today:

   var FOO = 42;
   function bar(x, y) {
     if (y === void 0) y = FOO + x;
     ...
   }

ES6 as proposed:

   var FOO = 42;
   function bar(x, y = FOO + x) {
     ...
   }

What's the problem?

The other cases you show are all required to work today, including those 
with local var FOO and var y. We can't break compatibility.

Default parameters only enter into the picture if you think default 
parameter syntax should trigger new semantics (new scope contour(s) for 
parameter binding, new hoisting boundary in between parameters and body 
bindings, etc.).

New semantics for new syntax are certainly possible, but anything more 
complicated needs strong justification.

If you really want the Pythonic default parameter rules (evaluation in 
definition context) then you need to address the bad case Jason showed:

   function foo(arg = []) {
     arg.push('strange');
     return arg;
   }
   foo(); // ['strange']
   foo(); // ['strange', 'strange']

This is a side channel and a big footgun. ES6 default parameters as 
proposed avoid it.

/be


More information about the es-discuss mailing list