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 function
s in if
statement'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/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.