substitutes for arguments and arguments.callee in ECMAScript 5

Allen Wirfs-Brock allen at wirfs-brock.com
Wed Jan 5 12:48:36 PST 2011


Just a couple additional points to make sure that Brendan and I playing tag-team aren't sowing further confusion about this topic:

 ...subscribe(function func() {...})

according to the ES standard is a different construct from either

>> var foo = function() { ... }
or
>> function foo() { ... }

In particularly, the latter two introduce a declaration for the name "func" into the surrounding scope so it can be referenced anywhere in that scope.  The first function expression form introduces a declaration for "func" that is supposed to only be visible from within the {...} function body. Also, the function expression form has a well-defined meaning anywhere including in the compound statement blocks such as if-statements.  The meaning of the latter two declaration forms are not defined by the standard when they occur within compound statement blocks.  What they do, depends upon the browser.

IE, prior to IE9, didn't conform to the standard for function expressions and makes the name "func" visible in the surrounding scope.  However, if all you want to do if be able to refer to "func" within the {...} body and not interfere with any declarations in the surrounding scope, even for IE <= 8, you can usually accomplish this by choosing a unique name instead of "func" that is highly unlike to be used in the surrounding scope.  For example,

 ...subscribe(function myLikelyUniqueName() {...myLikelyUniqueName...})

Allen

On Jan 5, 2011, at 12:10 PM, Brendan Eich wrote:


> 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
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

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


More information about the es-discuss mailing list