Function inside if statement?

Garrett Smith dhtmlkitchen at gmail.com
Tue Feb 5 15:45:13 PST 2008


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:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Functions#Conditionally_defining_a_function

What should the above code do?

Should it be:

if(a) {
 (function b(){ });
}

SHould it be a function declaration, just like a variable statement?

if(a) {
 var b;
}

Spidermonkey treats |b| as a function declaration as a statement (example).

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

Garrett



More information about the Es4-discuss mailing list