【发布时间】:2013-06-10 17:51:13
【问题描述】:
据我所知,以下声明不会向变量aa 添加任何值:
var aa = undefined;
function a () {
var aa;
console.log(aa); // here aa is still undefined
if(!aa) {
aa = 11; // should add to the globle scope (window Object)
bb = 12; // should add to the globle scope (window Object)
}
console.log(aa);
console.log(aa); // should be 11
console.log(bb); // should be 12
}
现在如果我想使用访问变量aa 和bb,我只能访问bb 而不是aa。
我的问题是为什么不能从外部访问aa,因为在声明中我没有为它分配任何值并且它仍然是未定义的?
谢谢。
【问题讨论】:
-
您已将 aa 重新定义为在函数范围内,并且由于您没有分配值,因此它已分配 undefined
-
您正在函数中重新声明变量 aa
-
@JonathandeM.:你的意思是
undefined是分配给变量的值吗?
标签: javascript scope global-variables undefined window-object