On Wed, Jan 5, 2011 at 12:48 PM, Allen Wirfs-Brock <al...@wirfs-brock.com>wrote:

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

The "latter two"? The function declaration form is indeed ambiguous as
currently implemented across browsers. (And this ambiguity does not violate
the spec, as such nested function declarations are outside the std grammar
and so allowed under the Ch16 exemptions.) However, the "var" declaration
form has no such ambiguity. The spec mandates var hoisting for the
visibility of the "foo" variable, and that the initialization be equivalent
to an assignment to "foo" occurring according to the textual placement of
the initialization. In other words,

     function bar() {
         ...
         if (t) {
             ...
             var foo = function() { ... };
             ...
         }
         ...
     }

is equivalent to

     function bar() {
         var foo;
         ...
         if (t) {
             ...
             foo = function() { ... };
             ...
         }
         ...
     }

AFAIK, all browsers conform to the spec in this regard.



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


-- 
    Cheers,
    --MarkM
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to