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

begin:vcard
fn:Laurens Holst
n:Holst;Laurens
email;internet:[email protected]
version:2.1
end:vcard

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to