检测数据类型的方式

第一题、检测数据类型的方式
第一种:
typeof 方法  console.log(type 内容)
typeof 可以检测基本类型,但是碰到引用数据类型均返回为object。
第二种:
instanceof 方法 console.log(内容 instanceof 数据类型)
instanceof可以用于引用类型的检测,但对于基本类型是不生效的,另为,不能用于检测null和undefined
第三种:
constructor 用法 console.log(内容.constructor===数据类型)
撇去null和undefined,constructor能用于检测js的基本类型和引用类型,但当对象的原型更改之后,constructor便失效了
第四种
Object.prototype.toString.call() 用法 console.log(Object.prototype.toString.call(判断的内容))
可用于检测js所有数据类型

原文地址:https://www.cnblogs.com/bigbang66/p/13550051.html