嘿,我正在尝试理解 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
我期待所有的输出都是,但true
在第一行。为什么是这样?a
undefined
woot()