(function(){
var a=b=6;
console.log(typeof a);
console.log(typeof b);
})();
console.log(typeof a);
console.log(typeof b);
输出为:
number
number
undefined
number
第三行为undefined?
如果第三行为undefined第四行为什么是number?
var a = b = 6;
实际上等同于这样:
var a = 6; b = 6;
因为变量
b
没有关键字声明,所以被注册成为了全局变量。所以
a
是匿名函数里的局部变量,局外找不到,输出为undefined
b
被注册成为了全局变量,所以能在外面找到,输出为number