I disagree with the other answers that say this is valid.
According to the ECMA-262 specification, the 5th Edition Blocks can only contain Statements ( Section 12.1 ):
Block : { StatementList opt } StatementList : Statement StatementList Statement
However, the specification does not define a function operator, but only FunctionDeclaration and a FunctionExpression . The spectrum goes further to mark this in Section 12 :
It is known that several commonly used ECMAScript implementations support the use of FunctionDeclaration as Statement . However, there are significant and irreconcilable variations among implementations in semantics applied to such FunctionDeclarations . Due to this irreconcilable difference, using FunctionDeclaration as a Statement causes the code to not be reliably ported among implementations. It is recommended that ECMAScript implementations either prohibit this use of FunctionDeclaration or issue a warning when such use is encountered. Future releases of ECMAScript may define alternative portable means for declaring functions in the context of Statement .
For further reading, you may also be interested in learning the comp.lang.javascript FAQ Section 4.2 :
4.2 What is a function instruction?
The term function operator is widely and mistakenly used to describe FunctionDeclaration . This is misleading because in ECMAScript a FunctionDeclaration not a Statement ; There are places in the program where Statement allowed, but FunctionDeclaration not. To add to this confusion, some implementations, notably Mozillas', provide a syntax extension called a function statement. This is permitted by section 16 of ECMA-262, editions 3 and 5.
An example of a non-standard function:
// 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 a function operator has three well-known interpretations. Some implementations execute Fze as an expression, in order. Others, including JScript, evaluate Fze when entering the execution context in which it appears. Others, in particular DMDScript and the standard BESEN configuration, throw a SyntaxError .
For consistent behavior in all implementations, do not use a function statement; use FunctionExpression or FunctionDeclaration instead.
FunctionExpression example (valid):
var Fze; try { Fze = function(b,a){return b.unselectable=a}; } catch(e) { _DumpException(e) }
FunctionDeclaration example (valid):