JS的数据类型检测

2020-02-05  本文已影响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():检测数据类型最好的方法

上一篇下一篇

猜你喜欢

热点阅读