Generator issue: exceptions while initializing arguments

Brendan Eich brendan at mozilla.com
Tue Sep 11 01:36:14 PDT 2012


Dmitry Soshnikov wrote:
> var FOO = 42;
> function bar(x, y = FOO + z) { var z = 10; } // Error .. ?

Translating to JS as it is:

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

No error expected, undefined hoisted z used in FOO + z resulting in y 
defaulting to NaN if y is not passed or given actual undefined value in 
a call.

> P.S.: Though, it still seems that the parent-only scope fits better 
> since avoids complexities. Otherwise, we'll get yet another 
> interesting quiz questions like this one:
>
> var x = 10;
>
> (function foo() {
>   console.log(x); // undefined?
>   var x = 20;
> })();

This case has *nothing* to do with default parameters. The answer is of 
course undefined and we can't change that. I'm not sure why you bring it up.

/be


More information about the es-discuss mailing list