The following javascript works in Chrome and IE but errors out in Firefox because bar is undefined when assigned to callBar.
So who got their scope rules right?
function foo() {
var callBar = bar;
if (1 === 1) {
callBar();
function bar() {
alert('yo');
}
}
}
foo();
Not sure who got it “right” according to the ECMA spec, but it doesn’t really matter since you can’t do this in all browsers and have to change your code 🙂
The simple explanation is that
functions inifstatement’s aren’t technically allowed and browsers do weird things with them. Some browsers treat this as an expression, others as a declaration. FF apparently treats it as an expression meaning it doesn’t exist until the code gets to that point. A detailed write-up is available at http://kangax.github.com/nfe/