前端如何判断数据类型?

38 阅读1分钟

前端如何判断数据类型

  1. typeof 可以判断除了null 之外的所有值类型,以及判断function【特别注意 type null === 'object' 是 true】

  2. instanceof 比较引用类型(对象)

  3. Object.prototype.toString.call()

 Object.prototype.toString.call('xx') === '[object String]'
 Object.prototype.toString.call([]) === '[object Array]'
  1. 下一篇介绍instanceof实现的原理