Firefox doesn’t hoist function declarations in blocks
statichtml.com
statichtml.com
Worth checking btw if JS strict mode fixes this. I know it fixes related issues.
"Firefox doesn’t actually issue a warning, which would have made my job tracking this issue down much easier. Moreover, functions declared within a block before being called work just fine, so it's not that function declarations can't by used as statements so much as SpiderMonkey won't bother to hoist them before executing any other code in that block."
As I said, the grammar in the ECMA-262 spec only allows Statements within Blocks, and a FunctionDeclaration isn't a Statement.
None of the browsers adhere to the spec, but I suspect they're just trying to play nicely with legacy content. Firefox just happens to play differently to all the other browsers.
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().