I found a bug in my site, because of this strange behavior. The second function alerts "undefined" instead of "one".
function test() {
var myParam = "one"
if (false) {
var myParam = "two";
}
alert(myParam);
}
test(); // alert "one"
function testAjax() {
var myParam = "one";
$.ajax({
url: "http://url-to-get-error",
error: function(){
if (false) {
var myParam = "two";
}
alert(myParam);
}
});
}
testAjax(); // alert "undefined"
And if I comment block bellow, alerts "one" correctly. Someone knows why?
if (false) {
var myParam = "two";
}
See in http://jsfiddle.net/EaW8D/2/.
UPDATE
Guys, I know how to to solve the problem, thanks a lot!
But the question is WHY js is setting undefined in a block
that NEVER run (if (false)).