JavaScript数据类型判断之Object.prototype.toString.call() 的详解

Object.prototype.toString.call() 的详解

Object.prototype.toString.call() 是 JavaScript 中判断数据类型的强大方法。它可以精确区分所有的 JavaScript 内置类型,包括原始类型和对象类型。


语法

Object.prototype.toString.call(value)
  • value 是要检测类型的变量。
  • 返回结果是一个类似 "[object Type]" 的字符串,其中 Type 是该变量的类型名称。

为什么使用它?

  • 准确性:它可以区分 nullundefined,以及各种内置对象(如 ArrayDate 等)。
  • 通用性:不依赖环境或原型链,可以用于跨环境的类型判断。

返回值

以下是常见数据类型及其对应的返回值:

数据类型返回值
Number[object Number]
String[object String]
Boolean[object Boolean]
Undefined[object Undefined]
Null[object Null]
Array[object Array]
Object[object Object]
Function[object Function]
Date[object Date]
RegExp[object RegExp]
Error[object Error]
Symbol[object Symbol]
Map[object Map]
Set[object Set]
WeakMap[object WeakMap]
WeakSet[object WeakSet]
BigInt[object BigInt]
自定义类实例[object Object]

示例

// 基本类型
console.log(Object.prototype.toString.call(123)); // [object Number]
console.log(Object.prototype.toString.call("hello")); // [object String]
console.log(Object.prototype.toString.call(true)); // [object Boolean]
console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
console.log(Object.prototype.toString.call(null)); // [object Null]

// 对象类型
console.log(Object.prototype.toString.call([])); // [object Array]
console.log(Object.prototype.toString.call({})); // [object Object]
console.log(Object.prototype.toString.call(function() {})); // [object Function]
console.log(Object.prototype.toString.call(new Date())); // [object Date]
console.log(Object.prototype.toString.call(/abc/)); // [object RegExp]

// 特殊对象
console.log(Object.prototype.toString.call(new Map())); // [object Map]
console.log(Object.prototype.toString.call(new Set())); // [object Set]
console.log(Object.prototype.toString.call(Symbol())); // [object Symbol]
console.log(Object.prototype.toString.call(BigInt(123))); // [object BigInt]

适用场景

  1. 区分 nullobject

    console.log(typeof null); // "object" (误导性结果)
    console.log(Object.prototype.toString.call(null)); // [object Null]
    
  2. 区分对象类型

    console.log(Object.prototype.toString.call([])); // [object Array]
    console.log(Object.prototype.toString.call({})); // [object Object]
    
  3. 跨环境检测
    在不同的 JavaScript 环境中(如浏览器和 Node.js),使用 instanceof 有时可能会失败,而 Object.prototype.toString.call() 是可靠的。


封装为通用函数

可以封装为一个类型检测工具:

function getType(value) {
  return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}

console.log(getType(123)); // "number"
console.log(getType(null)); // "null"
console.log(getType([])); // "array"
console.log(getType(new Map())); // "map"
console.log(getType(() => {})); // "function"

注意事项

  • 自定义类实例(非内置对象)都会返回 [object Object],如果需要区分,可以结合 constructor.name

    class MyClass {}
    const instance = new MyClass();
    console.log(Object.prototype.toString.call(instance)); // [object Object]
    console.log(instance.constructor.name); // MyClass
    
  • 无法检测用户定义的 Symbol.toStringTag 修改:

    const obj = { [Symbol.toStringTag]: "CustomTag" };
    console.log(Object.prototype.toString.call(obj)); // [object CustomTag]
    

Object.prototype.toString.call() 是一种强大的数据类型判断方法,特别适用于复杂类型判断。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值