js中经常用到数据类型检查,常用的类型检查方法有typeof、instanceof、constructor、Object.prototype.toString.call等,现在逐一介绍一下。
js中数据类型总体上分为二大类:基本类型(原始数据类型)和引用类型,其中
基本类型又分为常用的:
string、number、boolean、null、undefined、symbol、BigInt这些类型
引用类型是除去基本类型外的,比如常用的Array、Object这些
一、typeof
1. 使用typeof判断基本类型:
const str = 'testme'
typeof str // stringconst num = 123
typeof num // numberconst bol = true
typeof bol // booleanconst nu = null
type nu // objecttypeof undefVar // undefinedconst sy = Symbol('a')
typeof sy // symbolconst bi = BigInt("12345678910111213")
typeof bi // bigint2. 使用typeof判断引用类型:const arr = [1,5,7]
typeof arr // objectconst obj = {a:3}
typeof obj // objectfunction Man(name,age) {this.name = namethis.age = age
}
typeof Man // functionconst man = new Man('Lily', 12)
typeof man // object
二、instanceof
使用方法: A instanceof B
主要是确认B.prototype属性是否在A的原型链上,如果一直顺着原型链找到Object.prototype还是没找到,结果就返回false。主要用来判断引用数据类型的:
const arr = [1,5,7]
arr instanceof Array // trueconst obj = {a:3}
obj instanceof Object // trueobj instanceof Array // falsefunction Man(name,age) {this.name = namethis.age = age
}Man instanceof Function // trueconst man = new Man('Lily', 12)
man instanceof Object // true
三、constructor
A.constructor主要可以返回A对应的构造函数:
const str = 'testme'
str.constructor === String // trueconst num = 123
num.constructor === Number // trueconst bol = true
bol.constructor === Boolean // trueconst arr = [1,5,7]
arr.constructor === Array // trueconst obj = {a:3}
obj.constructor === Object // truefunction Man(name,age) {this.name = namethis.age = age
}Man.constructor === Function // trueconst man = new Man('Lily', 12)
man.constructor === Man // true但是像 null、undefined这种就调用不了constructor判断数据类型了
四、Object.prototype.toString.call
Object.prototype.toString.call方法返回各数据类型的[object xxx]形式:
const str = 'testme'
Object.prototype.toString.call(str) // [object String]const num = 123
Object.prototype.toString.call(num) // [object Number]const bol = true
Object.prototype.toString.call(bol) // [object Boolean]const sy = Symbol('a')
Object.prototype.toString.call(sy) // [object Symbol]const bi = BigInt("12345678910111213")
Object.prototype.toString.call(bi) // [object BigInt]Object.prototype.toString.call(null) // [object Null]Object.prototype.toString.call(undefined) // [object Undefined]const arr = [1,5,7]
Object.prototype.toString.call(arr) // [object Array]const obj = {a:3}
Object.prototype.toString.call(obj) // [object Object]function Man(name,age) {this.name = namethis.age = age
}Object.prototype.toString.call(Man) // [object Function]const man = new Man('Lily', 12)
Object.prototype.toString.call(man) // [object Object]
可以看出Object.prototype.toString.call方法判断的数据类型更广。
去掉Object.prototype.toString.call方法返回结果中的"[object",只保留具体类型:
function getType(data) {return Object.prototype.toString.call(data).replace(/\[object\s+(.+)\]/, '$1' ).toLowerCase()
}const str = 'testme'
getType(str) // stringconst num = 123
getType(num) // numberconst bol = true
getType(bol) // booleanconst sy = Symbol('a')
getType(sy) //symbolconst bi = BigInt("12345678910111213")
getType(bi) // bigIntgetType(null) // nullgetType(undefined) // undefinedconst arr = [1,5,7]
getType(arr) // arrayconst obj = {a:3}
getType(obj) // objectfunction Man(name,age) {this.name = namethis.age = age
}getType(Man) // functionconst man = new Man('Lily', 12)
getType(man) // object