<分区>
我理解以下函数,因为发生了变量提升。 foo();
function foo() {
console.log( a ); // undefined
var a = 2;
}
但是我不明白的是以下部分。 我收到引用错误,为什么?
foo()
function foo() {
console.log( a ); // Reference Error
a = 2;
}
--- 编辑---
到目前为止,我从答案中了解到第二个没有进行任何提升,我们不能使用任何 undefined variable 。
foo()
function foo() {
// we cannot use any undefined variable, which "a" here
console.log( a );
window.a = 2;
}
例如
var a;
a; //undefined
b; //Reference error