美文网首页
JS数据类型判断

JS数据类型判断

作者: shandamengcheng | 来源:发表于2021-08-03 23:30 被阅读0次

    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

    相关文章

      网友评论

          本文标题:JS数据类型判断

          本文链接:https://www.haomeiwen.com/subject/gjselktx.html