var scope = "global";
function f(){
alert(scope);//1
var scope="local";
alert(scope);//2
}
f();
javascript權威指南上的一段程序,語句1的scope是undifined。
局部變量的作用域是整個函數體,這本身也沒什麼,但是爲什麼初始值不搞成函數體呢(難道是因爲是解釋的)?
var scope = "global";
function f(){
alert(scope);//1
var scope="local";
alert(scope);//2
}
f();