JS数据类型判断

2021-08-03  本文已影响0人  shandamengcheng

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
上一篇下一篇

猜你喜欢

热点阅读