issue: function hoisting and parameter default value initialization

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Oct 10 08:59:14 PDT 2012


On Oct 10, 2012, at 6:33 AM, Andreas Rossberg wrote:

> 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.
> 


You are getting hung up on your desugaring, and loosing sight of the actual semantics we need to specify. In fact I won't use a desugaring to specify any of this.

length.arguments is the number of actual arguments provided at the call site.  It has nothing to do with the form of the formal parameter list.

All of the above (body declarations not visible to default value expressions, handling of destructuring, correctly setting up arguments) are all things that I can handle using current specification techniques.

However, the above does point out that if you try to use desugaring as an implementation technique there are a number of issue will have to carefully address.

Allen




More information about the es-discuss mailing list