substitutes for arguments and arguments.callee in ECMAScript 5

Brendan Eich brendan at mozilla.com
Wed Jan 5 12:10:26 PST 2011


On Jan 5, 2011, at 12:06 PM, Felipe Gasper wrote:

> On 1/5/11 1:43 PM, Allen Wirfs-Brock wrote:
>> On Jan 5, 2011, at 11:30 AM, Felipe Gasper wrote:
>> 
>>> Am I to understand now that ECMASCript 5 will now have me type in:
>>> 
>>> ----
>>> var func;
>>> a_dialog.hideEvent.subscribe( func = function() {
>>> this.hideEvent.unsubscribe( func );
>>> console.log("This will only happen once.");
>>> } );
>> 
>> No, what you should type is:
>> 
>> a_dialog.hideEvent.subscribe( function func() {
>> this.hideEvent.unsubscribe( func );
>> console.log("This will only happen once.");
>> } );
> 
> Ah - thanks.
> 
> That would seem to work in most cases, but the subtle differences between
> 
> var foo = function() { ... }
> 
> and
> 
> function foo() { ... }
> 
> ...make me a little uncertain. Crockford says in “The Good Parts” that “it turns out that *most* browsers allow function statements in if statements” (p113, emphasis added), but he doesn’t elaborate on which browsers that does/doesn’t mean. And there are differences of scope between the two declarations of the function that seem to invite subtle bugs.

Allen's revision of your example is not using any extension to ES3. It's using a named function expression.

Functions in blocks are an extension, implemented variously.

Unfortunately, even named function expressions have a notorious bug up through IE8 (IIRC) where the name is bound in the variable object.

/be

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110105/3f182e74/attachment.html>


More information about the es-discuss mailing list