判断数据类型
Contents
总结
Object.prototype.toString.call(obj)
最准确。- typeof 只能检测基本数据类型。
利用
typeof
来判断number
,string
,object
,boolean
,function
,undefined
,symbol
这七种类型null会判断为’object’,引用类型除了函数外其他都会被判断为’object'
- instanceOf 只能检测引用数据类型
总结
适用于 | 返回 | |
---|---|---|
typeof | 基本数据类型 | string |
instanceof | 引用数据 | true/false |
Object.prototype.toString |
都可以 | string |
Object.prototype.toString.call(obj)
原理
返回 obj 所属类的信息。
- 基本类型数据原型上的toString方法都是把当前的数据类型转换为字符串的类型(它们的作用仅仅是用来转换为字符串的)
- 引用类型数据上的toString返回当前方法执行的主体(方法中的this)所属类的信息即
[object Object]
=。
|
检验方法
Object.prototype.toString.call(a).split(' ')[1].slice(0,-1).toLowerCase()
typeof
原理
基于js底层存储变量数据类型的值(二进制)进行检测
类型 | typeof 结果 | |
---|---|---|
基本类型 | undefined | “undefined” |
Boolean | “boolean” | |
Number | “number” | |
String | “string” | |
BigInt (ECMAScript 2020 新增) | “bigint” | |
Symbol | “symbol” | |
null | “object” | |
引用类型 | Object(Object、Array、Map、Set等) | “object” |
Function | “function” |
对于原始类型来说,除了 null 都可以调用 typeof 显示正确的类型。null会被检测为object,是js底层的一个bug。
语法
typeof检测null是一个对象
typeof检测函数返回时一个function
typeof检测其他对象都返回 object
|
但对于引用数据类型,除了函数之外,都会显示"object"。
|
instanceof
原理
判断当前类出现在实例的原型链上。
表达式为:A instanceof B,如果A是B的实例,则返回true,否则返回false。
语法
|
弊端
-
不能检测null 和 undefined
const arr = [1, 2, 3]; console.log(arr instanceof null)//Uncaught TypeError: Right-hand side of 'instanceof' is not an object console.log(arr instanceof undefined)//Uncaught TypeError: Right-hand side of 'instanceof' is not an object
对于特殊的数据类型null和undefined,他们的所属类是Null和Undefined,但是浏览器把这两个类保护起来了,不允许我们在外面访问使用。
- 对于基本数据类型来说,字面量方式创建出来的结果和实例方式创建的是有一定的区别的,所以不能检测基本类型数据。
|
- 只要在当前实例的原型链上,我们用其检测出来的结果都是true。在类的原型继承中,我们最后检测出来的结果未必准确。
|
源码实现
|