3 ms·
>The grammar reference section states that the only constructs allowed within a Block are Statement constructs, and that a FunctionDeclaration isn’t a Statement
by skimbrel 15y ago
>The grammar reference section states that the only constructs allowed within a Block are Statement constructs, and that a FunctionDeclaration isn’t a Statement.
Huh! Never knew that. I wonder what their reasoning was when they decided that. Behavior in this case is all over the map in other languages: C and Python both consider function declarations and definitions as statements, but C allows you to provide a forward declaration of a function that you define later, after invoking the function, while Python doesn't. Perl allows forward references to subroutines without a preemptive declaration; Java considers method declarations as separate grammatical entities from statements but doesn't care what order you put them in within a class; Ruby behaves similarly to Perl.
PHP (as always) is a total odd-man-out: it considers a function declaration to be a statement, doesn't allow forward references to functions, and it exposes nested function definitions to the current namespace as soon as the parent function has been executed once. That is, if you define a function foo() and inside its body define bar(), you can access bar() from outside of foo(), but only once you have invoked foo().