[javascript核心-09] 彻底解决js中的类型检测方案

文章详细介绍了JavaScript中用于检测数据类型的几个关键方法,包括typeof、instanceof和constructor,以及使用Object.prototype.toString.call进行更精确的类型判断。还提出了最佳实践,即结合typeof和Object.prototype.toString.call来实现全面的数据类型检测,并提供了一个封装的toType函数示例,能够检测包括Number、String、Boolean等在内的所有基本类型和复杂类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

typeof
  1. 基于数据类型的值(二进制)进行检测
  2. 返回结果为字符串
  3. typeof NaN结果为number
  4. typeof null结果为Object.对象存储以000开头,而null也是如此。
  5. typeof不能细分对象,结果都是Object
  6. typeof function(){}结果为function
instanceof
  1. 检测某个构造函数是否出现在某实例的原型链上
  2. 返回结果为boolean值
  3. [] instanceof Arraytrue, [] instanceof Objecttrue。原型可手动修改,因此检测结果也会被篡改。
  4. 不能检测基本数据类型。1 instanceof Numberfalse
constructor
  1. 检测某个函数是否是某实例的构造函数
  2. 返回结果为boolean值
  3. 可以检测基本数据类型
  4. constructor可手动修改,因此检测结果也会被篡改。
Object.prototype.toString.call()
  1. 返回当前实例所属类的信息
Object.prototype.toString.call(1) // '[object Number]'
Object.prototype.toString.call('1') // '[object String]'
Object.prototype.toString.call(true) // '[object Boolean]'
Object.prototype.toString.call(null) // '[object Null]'
Object.prototype.toString.call(undefined) // '[object Undefined]'
Object.prototype.toString.call(Symbol(1)) // '[object Symbol]'
Object.prototype.toString.call(/^/) // '[object RegExp]'
Object.prototype.toString.call(new Date) // '[object Date]'
Object.prototype.toString.call([]) // '[object Array]'
Object.prototype.toString.call({}) // '[object Object]'
Object.prototype.toString.call(()=>{}) // '[object Function]'
Object.prototype.toString.call(1n) // '[object BigInt]'
Object.prototype.toString.call(new Error()) // '[object Error]'
最佳实践
  1. typeof 可以检测基本数据类型的值(除 Null 以外)
  2. 其他的类型可以用Object.prototype.toString.call()
封装数据类型检测的方法

目标:能够对Number,String,Boolean,Null,Undefined,Symbol,RegExp,Date,Array,Object,Function,Error,BigInt全部类型值进行细分检测。

类型检测函数

function toType(obj){
    const classType = {};
    ['Number','String','Boolean','Null','Undefined','Symbol','RegExp','Date','Array','Object','Function','Error','BigInt'].forEach(name => {
        classType[`[object ${name}]`] = name.toLowerCase()
    });

    function _toType(obj){
        if(obj == null) return obj + ''
        return typeof obj === 'object' || typeof obj === 'function' ? classType[toString.call(obj)] || 'object' : typeof obj
    }
    return _toType(obj)
}

进行测试:

// 测试
[1,'1',true,null,undefined,Symbol(1),/^/,new Date(),[],{},()=>{},new Error(),100n].forEach(obj => {
    console.log(toType(obj))
})

/*
number
string
boolean
null
undefined
symbol
regexp
date
array
object
function
error
bigint
*/

建立映射表进行比对:

const map = [
    [1,'number'],
    ['1','string'],
    [true,'boolean'],
    [null,'null'],
    [undefined,'undefined'],
    [Symbol(1),'symbol'],
    [/^/,'regexp'],
    [new Date(),'date'],
    [[],'array'],
    [{},'object'],
    [()=>{},'function'],
    [new Error(),'error'],
    [100n,'bigint']
]

for (const tuple of map) {
    console.log(toType(tuple[0]) === tuple[1])
}

/*
true
true
true
true
true
true
true
true
true
true
true
true
true
*/

本文github地址:JavaScript_Everything 大前端知识体系与面试宝典,从前端到后端,全栈工程师,成为六边形战士

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值