嘿,所有我試圖理解JavaScript封鎖和對這段代碼一個簡單的問題,的Javascript閉包和內部範圍
var a = 5;
function woot() {
console.log('a == 5 woot: ' + (a == 5).toString());
var a = 6;
function test() {
console.log('a == 6 test: ' + (a == 6).toString());
}
test();
}
console.log('a == 5 outside: ' + (a == 5).toString());
woot();
console.log('a == 5 end: ' + (a == 5).toString());
輸出:
a == 5 outside: true
a == 5 woot: false
a == 6 test: true
a == 5 end: true
我期待在woot()
的第一行中,所有輸出爲true
,但是a
爲undefined
。爲什麼是這樣?