主要有两种类型检测
typeof 以及 instanceof
一、typeof 判断类型 返回一个字符串
typeof——数字>>>>>>>number
typeof——布尔值>>>>>>>boolean
typeof——字符串>>>>>>>object
typeof——undefined>>>>>>>undefined
typeof——null>>>>>>> object typeof null===object
typeof——NaN>>>>>>> number
typeof——数组>>>>>>> object
typeof——对象>>>>>>> object
typeof——function>>>>>>> function
二、instanceof 基于原型链判断
instranceof 的左操作数必须是一个obj类型 ,右操作数必须是一个函数对象。否则 返回false。
它判断左操作数在原型链上是否有右操作数的prototype属性。
三、Object.prototye.toString
四、constructor
五、duck type
类型检测小结:
typeof 适合检测基本类型和function,遇null失败。
instanceof 适合检测自定义对象,也可以在用来检测原生对象。
Object.prototye.toString 适合检测内置对象和基元类型。遇到null和undefined失效。