我正在阅读有关JavaScript执行上下文和作用域的主题。以下是一段简单的代码:
var scope="global";
function t(){
alert(scope); // alert :"undefined"
var scope="local" ;
alert(scope); // alert: "local"
}
t();
如果我去掉 'var scope="local" ;' ,就变成了这个:
var scope="global";
function t(){
alert(scope); // alert :"global"
}
t();
在函数t()中删除 var scope="local" 后,我不明白为什么第二个例子中scope的值会变为“global”。
有人能帮忙解释一下吗?谢谢!