Reference: JavaScript - The Definitive Guide, Section 4.3
function 內宣告的 var scope 是整個 function 的,舉例如下:
就是說,這時候下面那行的 scope 已經宣告了,只是還沒初始。
下圖是書中所給的 scope chain 示意圖:

function 內宣告的 var scope 是整個 function 的,舉例如下:
var scope = "global";
function f( ) {
alert(scope); // Displays "undefined", not "global"
var scope = "local"; // Variable initialized here, but defined everywhere
alert(scope); // Displays "local"
}
f( );
第一個 alert(scope); 會 undefine 是因為下面那一行宣告的關係,就是說,這時候下面那行的 scope 已經宣告了,只是還沒初始。
下圖是書中所給的 scope chain 示意圖: