Expression closures - use-cases for shortcut lambda syntax(blocks)

Brendan Eich brendan at
Mon Mar 19 15:46:20 PDT 2007

On Mar 19, 2007, at 3:36 PM, Brendan Eich wrote:

> you have written a useless unnamed function expression statement  
> that ends at the } that closes the body, and any () after will  
> provoke a syntax error:
> js> function(){return 2}()
> typein:4: SyntaxError: syntax error:
> typein:4: function(){return 2}()
> typein:4: .....................^
> This is due to
> 12.4 Expression Statement
> Syntax
> ExpressionStatement :
> [lookahead ∉ {{,function}] Expression ;
> in ECMA-262 Edition 3.

But of course, the chapter and verse I cite, in conjunction with the  
rest of the grammar, makes even

js> function () {}

a SyntaxError. But it's not in SpiderMonkey, due to an old  
compatibility promise (see 
id=303723). Probably it could be made a SyntaxError with more work  
under the SpiderMonkey API.

Just wanted to avoid sowing more confusion. ES3's use of function name 
(formals) block in several contexts, including in expressions where  
name is optional, may be the root confusion causer here. If back in  
1998 we had thought of the => alterative syntax for function  
expressions (unnamed ones -- named ones could use function name 
(formals) => assign-expr), we should have introduced that form then.  
It would have saved us the current trouble ;-).


-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Es4-discuss mailing list