Function inside if statement?

Brendan Eich brendan at
Tue Feb 5 15:53:36 PST 2008

On Feb 5, 2008, at 3:45 PM, Garrett Smith wrote:

> if(a) {
>  function b(){ }
> }
> A block can contain statements. A statement can't start with the
> function keyword.
> Mozilla's Core JavaScript guide explains that of |b| should be
> evaluated as a functionExpression, but this isn't right.
> Source:
> Core_JavaScript_1.5_Reference:Functions#Conditionally_defining_a_funct 
> ion

That doc is wrong. Wiki-elves, sigh.

SpiderMonkey has for about a decade implemented three kinds of  
function forms: definitions, expressions, and statements. This is an  
example of the last -- it's a function definition, syntactically,  
except produced as a child of another statement, possibly even a  
block -- a position which the ES3 grammar cannot produce a function  
definition. Only if control flow reaches the function statement does  
it bind its name.

This is an extension allowed by ES3 chapter 16.

> No imlementations I know throw a SyntaxError; instead, the behavior is
> implementation-specific.

Sure -- you knew that already by reading ES3, right? The problem with  
standardizing is diverting effort from other tasks, and then forging  
agreement on what function statements should mean. In IE, and IIRC  
Opera based solely on IE, they are function definitions -- they  
unconditionally bind their names on entry to the parent execution  
context. That seems like a mistake, but it's hard to tell given other  
IE bugs to do with named function forms (you know the one I mean well).


More information about the Es4-discuss mailing list