区分数组和对象的三种方法:
[].constructor //ƒ Array() { [native code] }
var obj = {};
obj.constructor//ƒ Object() { [native code] }
[] instanceof Array //true
var obj = {};
obj instanceof Array //false
Object.prototype.toString.call([])
//"[object Array]"
Object.prototype.toString.call(123)
//"[object Number]"
Object.prototype.toString.call({})
//"[object Object]"
经典例题:
var x = 1;
if(function f() {}) {
x += typeof f;
}
console.log(x) // 返回:1undefined;