optional "function" keyword

Brendan Eich brendan at mozilla.org
Tue Mar 6 12:37:34 PST 2012

Isaac Schlueter wrote:
> I see.  So, you'd be suggesting using:
>      ( args ) { functionBody }
> as an anonymous function expression, though not a function expression
> statement?

Right, see followup. The grammar without lookahead restrictions is 
ambiguous. See ES5,

12.4 Expression Statement


ExpressionStatement :
     [lookahead ∉ {{, function}] Expression ;

NOTE An ExpressionStatement cannot start with an opening curly brace 
because that might make it ambiguous with a Block. Also, an 
ExpressionStatement cannot start with the function keyword because that 
might make it ambiguous with a FunctionDeclaration.

Of course we cannot naively extend the lookahead rejection set to 
include ( because that forbids a top level parenthesized expression 
statement. One way around this is to refactor the grammar similarly to 
what I propose in


to separate InitialValue from AssignmentExpression, and then allow 
'function'-free function expressions as alternative right parts for 
IniitalValues. Thus you could write such shorthand function expressions 
in all places where you can write function expressions today, except 
unparenthesized followed by . or [ or another operator (+ might be 
useful, or confusing, sporadically).

This also allows us to extend the shorthand syntax to support an 
expression body as well as a braced statement list:


     LeftHandSideExpression = ShortFunctionExpression

     Identifier_opt ( FormalParameterList_opt ) { FunctionBody }
Identifier_opt ( FormalParameterList_opt ) IniitialValue

without precedence inversion problems.

>   What about these?
>      ;(( args ) { functionBody })
>      !( args ) { functionBody }

All fine, just as if you'd used the hated eight-letter keyword in front 
of the (.

> Certainly, this is quite nice:
>      myList.forEach((item) { .. })

Less nice for map because of the hated six-letter keyword, but yeah.


More information about the es-discuss mailing list