变量前不加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 ,即访问一个对象里不存在的值

转自https://www.jianshu.com/p/2db5364be59e