JS暗示全局变量
变量前不加var 如 b这个变量,就是暗示全局变量,存储在window这个全局作用域对象里
var a = 1;
b = 2;
console.log(window.a)
a === window.a // true
test函数调用后,会将b变量提升为全局变量
function test() {
var a = b = 1;
}
test();
console.log(b);
两种不同的访问方式,导致不同的输出结果
function test() {
var a = b = 1;
}
test();
console.log(a); // 抛错,ReferenceError: a is not defined
console.log(window.a); // undefined ,即访问一个对象里不存在的值
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。