issue: function hoisting and parameter default value initialization

Andreas Rossberg rossberg at google.com
Wed Oct 10 06:33:46 PDT 2012


On 9 October 2012 01:55, Brendan Eich <brendan at mozilla.org> wrote:
> Andreas Rossberg wrote:
>> Let me try again. How about the following desugaring?
>>
>>    function f(x1 = e1, ~~~, xN = eN) { body }
>>
>> means
>>
>>    function f(x1, ~~~, xN) {
>>      if (x1 === undefined) x1 = e1;
>>      ~~~
>>      if (xN === undefined) xN = eN;
>>      return (function(x1, ~~~, xN) { body })(x1, ~~~, xN);
>
>
> Nit: need (function(x1, ~~~ xN) { body }).call(this, x1, ~~~ xN) and not a
> direct this-free call.

Yes, indeed. Also, destructuring parameters of course require more work.


> This still is seems observable, because arguments.length will always be N,
> even when f is called with < N actual parameters.
>
> The fix that uses apply:
>
> function f(x1, ~~~, xN) {
>     if (x1 === undefined) x1 = e1;
>     ~~~
>     if (xN === undefined) xN = eN;
>     return (function(x1, ~~~ xN) { body }).apply(this, arguments);
> }
>
> seems to suffice (check me on this!).

OK, tell me how this is supposed to work. :) If arguments.length = M <
N, then how are xM...XN-1 possibly bound to eM...eN-1 in the body with
this version?

As an aside, did we actually ever talk about the interference of
defaults with 'arguments'? I'm not even sure what makes sense here. If
it wasn't for 1JS, I'd say don't provide 'arguments' at all in a
function with defaults.

/Andreas


More information about the es-discuss mailing list