可能的重复:
可变提升
var a = "global";
//version 1
function changeGlobal(){
alert(a); //why is 'a' undefined here?
if(a){
var a = "local";
alert(a);
}
}
//version 2
function changeGlobal(){
alert(a); //why is 'a' 'global' here? what diff does 'var a="local"' make?
if(a){
//var a = "local";
alert(a);
}
}
changeGlobal();
问题是内联的。帮助我理解变量范围。