JS数据类型判断

JS中判断数据类型的方式(已知8种)

2021了,这些判断方式你都知道吗

let arr = new Array();
  1. typeof

常见的判断类型的方式,但是对于引用类型,除了function,其他的都返回object,不是特别精确


image.png
  1. instanceof

这种方式的本质是根据原型链进行判断


image.png
  1. Symbol.hasIntance

其实当我们使用instanceof时,其深层的工作原理就是使用Symbol.hasInstance来进行判断。 即,在ES6中,instanceof操作符会使用Symbol.hasInstance函数来确定关系


image.png
  1. isPrototypeOf

每个Object都有一个基本的方法


image.png
  1. 数组专用 isArray


    image.png
  2. Object.prototype.toString.call()

将数据转换为字符串的形式


image.png
  1. constructor


    image.png
  2. arr.__ proto __ === Array.prototype


    image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容