美文网首页
JS的数据类型检测

JS的数据类型检测

作者: 苏码码 | 来源:发表于2020-02-05 17:22 被阅读0次

    JS的数据类型检测
    1、typeof:用来检测数据类型的运算符
    基于typeof检测出来的结果:首先是一个字符串,字符串中包含对应的类型
    局限性:
    1. typeof null =>"object" 但是null并不是对象
    2.typeof无法细分出当前值是数组对象还是普通对象,因为只要是对象类型返回的都是"object"
    typeof 1 => "number"
    typeof 'a' => "string"
    typeof [1,2] => "object"
    typeof NaN => "number"
    typeof {} => "object"
    typeof function(){} => "function"
    2、instanceof:用来检测当前实例是否属于某个类
    3、constructor:基于构造函数检测数据类型(也是基于类的方式)
    4、Object.prototype.toString.call():检测数据类型最好的方法

    相关文章

      网友评论

          本文标题:JS的数据类型检测

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