1.typeof检测数据类型
typeof [value] => 字符串,包含对应的数据类型
局限性: typeof null -> "object" 用===和null比较就可以了。
typeof 检测对象类型值,除了可执行对象{函数}可以检测出来是"function",其余都是"object"
typeof检测数据类型是,按照计算机底层存储的二进制值,来进行检测的,它认为以"000"开头的都是对象,而null都是零
2. instanceof检测数据类型
检测当前实例是否属于这个类
console.log({} instanceof Object) => true/false
缺点
-
instanceof不仅检测它作用的对象的构造函数,还是检测该对象的原型链的构造函数,只要有一个符合,就会返回true;比如:
console.log( [] instanceof Object); //true console.log( [] instanceof Array); //true
3. constructor
console.log(arr.constructor===Array);//=>true
4. Object.prototype.toString.call([value])
console.log(Object.prototype.toString.call({})) 返回当前实例所属的构造函数类型信息