经常用到的js类型检测方法
1.typeof检测类型
typeof 100 //返回 "number"
typeof true //返回 "boolean"
typeof function //返回 "function"
typeof (undefined) //返回 "undefined"
typeof new Object() //返回 "object"
typeof [1,2] //返回 "object"
typeof NaN //返回 "number"
typeof null //返回 "object"
2.使用instanceof判断对象类型(格式:instanceof 函数构造器)
[1,2] instanceof Array //返回true
new Object() instanceof Array //返回false
3.使用Object.prototype.toString
Object.prototype.toString.apply([]); //返回 "[object Array]"
Object.prototype.toString.apply(function(){}); //返回 "[object Function]"
Object.prototype.toString.apply(null); //返回 "[object Null]"
Object.prototype.toString.apply(undefined); //返回 "[object Undefined]"