js中几种常用数据类型检查方法

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 // string

const num = 123
typeof num // number

const bol = true
typeof bol // boolean

const nu = null
type nu // object

typeof undefVar // undefined

const sy = Symbol('a')
typeof sy // symbol

const bi = BigInt("12345678910111213")
typeof bi // bigint

2. 使用typeof判断引用类型:

const arr = [1,5,7]
typeof arr // object

const obj = {a:3}
typeof obj // object

function Man(name,age) {
   this.name = name
   this.age = age
}
typeof Man // function

const 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 // true

const obj = {a:3}
obj instanceof Object // true

obj instanceof Array // false

function Man(name,age) {
   this.name = name
   this.age = age
}

Man instanceof Function // true

const man = new Man('Lily', 12)
man instanceof Object // true

三、constructor

A.constructor主要可以返回A对应的构造函数:


const str = 'testme'
str.constructor === String // true

const num = 123
num.constructor === Number // true

const bol = true
bol.constructor === Boolean // true

const arr = [1,5,7]
arr.constructor === Array // true

const obj = {a:3}
obj.constructor === Object // true

function Man(name,age) {
   this.name = name
   this.age = age
}

Man.constructor === Function // true

const 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 = name
   this.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) // string

const num = 123
getType(num) // number

const bol = true
getType(bol) // boolean

const sy = Symbol('a')
getType(sy) //symbol

const bi = BigInt("12345678910111213")
getType(bi) // bigInt

getType(null) // null

getType(undefined) // undefined

const arr = [1,5,7]
getType(arr) // array

const obj = {a:3}
getType(obj) // object

function Man(name,age) {
   this.name = name
   this.age = age
}

getType(Man) // function

const man = new Man('Lily', 12)
getType(man) // object

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值