"Pretty" function expression names

Laurens Holst laurens.nospam at grauw.nl
Sun May 10 16:20:13 PDT 2009


Brendan Eich schreef:
>>> What I’m missing about Function.name is that you can only specify it 
>>> using function literals (at least, currently in Firefox I think that 
>>> is the case). It would be nice if you could also either set this 
>>> with either the Function constructor, or after the object has been 
>>> created.
>>
>> Ah, I see this part is already covered by Maciej’s Function.create 
>> semi-constructor proposal. In that case, should make sure this 
>> Function.create also supports passing closures so that we won’t need 
>> a Function.create2 :).
>
> Why would we ever want to mutate a closure? Please re-read
>
> https://mail.mozilla.org/pipermail/es-discuss/2009-March/008940.html

I’m not sure how that message applies to what I had in mind when writing 
the above sentence… We are probably talking about different things, I 
might have used the term ‘closure’ incorrectly.

If a free variable is used in a function literal it will look it up in 
the local scope of the enclosing function, effectively allowing you to 
pass a variable to a function at the time it is created which will be 
available at all invocations. This is not possible with a function 
constructor.

So it would be nice if you could do something like this:

function a() {
    var x = 1;
    return function y(z) {
       return x + z;
    }
}
var y = a();
y(2);


…by using a function constructor:

function a() {
    var x = 1;
    return Function.create('y', ['z'], {x: x}, 'return x+z;');
}
var y = a();
y(2);

Or perhaps you would rather want to pass a reference to the local scope 
(‘arguments’?) instead of an individual variables.

Now probably closures do not work exactly in this fashion, but this is 
just something else that functions literals can do and functions 
constructors currently can’t, making the latter a second-class citizen.

>> Maybe make the params parameter list an array so that create is less 
>> dependant on the length of the arguments passed and more extensible.
>
> This sounds like a good idea to me. The counter-argument is the 
> obligatory overhead of an array of parameter names. Others should 
> weigh in.
>
> One good thing about your proposal: it avoids (or fails to reuse, on 
> the down side) quirky standard behavior of the Function constructor. 
> Did you know ES1 specifies that new Function("a,b", "a+b") creates a 
> function similar to function anonymous(a, b) { return a + b; }? That 
> string-pasting feature of the Function constructor would seem to carry 
> over to Function.create, at first glance.

Yep. Crazy! :)

~Laurens

-- 
Note: New email address! Please update your address book.

~~ Ushiko-san! Kimi wa doushite, Ushiko-san nan da!! ~~
Laurens Holst, student, Utrecht University, the Netherlands
Website: www.grauw.nl. Backbase employee; www.backbase.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: laurens_nospam.vcf
Type: text/x-vcard
Size: 111 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090511/7165ee0c/attachment.vcf>


More information about the es-discuss mailing list