Please consider the official ECMAScript specification as the source for your answer, and not a document published by a specific browser vendor. (I am aware of Mozilla extend
I do not agree with the other answers that say it is valid.
According to the ECMA-262 5th Edition specification, Blocks can only contain Statements (Section 12.1):
Block :
{ StatementList opt }
StatementList :
Statement
StatementList Statement
However the spec does not define a function statement, but only a FunctionDeclaration and a FunctionExpression. The spec goes further to make a note on this in Section 12:
Several widely used implementations of ECMAScript are known to support the use of
FunctionDeclarationas aStatement. However there are significant and irreconcilable variations among the implementations in the semantics applied to suchFunctionDeclarations. Because of these irreconcilable difference, the use of aFunctionDeclarationas aStatementresults in code that is not reliably portable among implementations. It is recommended that ECMAScript implementations either disallow this usage ofFunctionDeclarationor issue a warning when such a usage is encountered. Future editions of ECMAScript may define alternative portable means for declaring functions in aStatementcontext.
For further reading, you may also be interested in checking out the comp.lang.javascript FAQ Section 4.2:
4.2 What is a function statement?
The term function statement has been widely and wrongly used to describe a
FunctionDeclaration. This is misleading because in ECMAScript, aFunctionDeclarationis not aStatement; there are places in a program where aStatementis permitted but aFunctionDeclarationis not. To add to this confusion, some implementations, notably Mozillas', provide a syntax extension called function statement. This is allowed under section 16 of ECMA-262, Editions 3 and 5.Example of nonstandard function statement:
// Nonstandard syntax, found in GMail source code. DO NOT USE. try { // FunctionDeclaration not allowed in Block. function Fze(b,a){return b.unselectable=a} /*...*/ } catch(e) { _DumpException(e) }Code that uses function statement has three known interpretations. Some implementations process
Fzeas a Statement, in order. Others, including JScript, evaluateFzeupon entering the execution context that it appears in. Yet others, notably DMDScript and default configuration of BESEN, throw aSyntaxError.For consistent behavior across implementations, do not use function statement; use either
FunctionExpressionorFunctionDeclarationinstead.Example of FunctionExpression (valid):
var Fze; try { Fze = function(b,a){return b.unselectable=a}; /*...*/ } catch(e) { _DumpException(e) }Example of FunctionDeclaration (valid):
// Program code function aa(b,a){return b.unselectable=a}