Object.prototype.toString()
toString() 方法是 Object 实例的一个方法,它返回一个表示该对象的字符串。此方法旨在由派生对象进行重写,以实现自定义的 类型转换 逻辑。
试一试
const map = new Map();
console.log(map.toString());
// Expected output: "[object Map]"
语法
toString()
参数
默认情况下,toString() 不接受任何参数。但是,继承自 Object 的对象可能会用自己的实现来重写它,这些实现可以接受参数。例如,Number.prototype.toString() 和 BigInt.prototype.toString() 方法接受一个可选的 radix 参数。
返回值
表示该对象的字符串。
描述
JavaScript 会调用 toString 方法来 将对象转换为原始值。你很少需要自己调用 toString 方法;当 JavaScript 遇到期望原始值的对象时,它会自动调用它。
此方法被 字符串转换 优先调用,但 数字转换 和 原始值转换 会优先调用 valueOf()。然而,由于基础的 valueOf() 方法返回一个对象,因此 toString() 方法通常会在最后被调用,除非对象重写了 valueOf()。例如,+[1] 返回 1,因为它的 toString() 方法返回 "1",然后将其转换为数字。
所有继承自 Object.prototype 的对象(即除 null-prototype 对象 之外的所有对象)都会继承 toString() 方法。当你创建一个自定义对象时,可以重写 toString() 来调用一个自定义方法,这样你的自定义对象就可以转换为字符串值。或者,你可以添加一个 [Symbol.toPrimitive]() 方法,它允许对转换过程进行更精细地控制,并且在任何类型转换中始终优先于 valueOf 或 toString。
要使用基础的 Object.prototype.toString() 方法处理一个已被重写的对象(或者在 null 或 undefined 上调用它),你需要对其调用 Function.prototype.call() 或 Function.prototype.apply(),并将你想要检查的对象作为第一个参数传递(称为 thisArg)。
const arr = [1, 2, 3];
arr.toString(); // "1,2,3"
Object.prototype.toString.call(arr); // "[object Array]"
Object.prototype.toString() 返回 "[object Type]",其中 Type 是对象的类型。如果对象有一个 Symbol.toStringTag 属性,并且其值为一个字符串,那么该值将作为 Type 使用。许多内置对象,包括 Map 和 Symbol,都有一个 Symbol.toStringTag。一些早于 ES6 的对象没有 Symbol.toStringTag,但仍然有一个特殊的标签。它们包括(标签与下面给出的类型名称相同):
arguments 对象返回 "[object Arguments]"。其他所有对象,包括用户定义的类,除非有自定义的 Symbol.toStringTag,否则将返回 "[object Object]"。
在 null 和 undefined 上调用 Object.prototype.toString() 分别返回 [object Null] 和 [object Undefined]。
示例
为自定义对象重写 toString
你可以创建一个函数来替代默认的 toString() 方法。你创建的 toString() 函数应该返回一个字符串值。如果它返回一个对象,并且该方法在 类型转换 期间被隐式调用,那么它的结果将被忽略,而是使用相关方法 valueOf() 的值,或者如果这些方法都不返回原始值,则会抛出 TypeError。
以下代码定义了一个 Dog 类。
class Dog {
constructor(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}
}
如果你显式或隐式地在 Dog 实例上调用 toString() 方法,它将返回从 Object 继承的默认值。
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");
theDog.toString(); // "[object Object]"
`${theDog}`; // "[object Object]"
以下代码重写了默认的 toString() 方法。此方法生成一个包含对象的 name、breed、color 和 sex 的字符串。
class Dog {
constructor(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}
toString() {
return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
}
}
有了前面的代码,当 Dog 的实例在字符串上下文中被使用时,JavaScript 会自动调用 toString() 方法。
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");
`${theDog}`; // "Dog Gabby is a female chocolate Lab"
使用 toString() 检测对象类
toString() 可与所有对象一起使用,(默认情况下)允许你获取其类。
const toString = Object.prototype.toString;
toString.call(new Date()); // [object Date]
toString.call(new String()); // [object String]
// Math has its Symbol.toStringTag
toString.call(Math); // [object Math]
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]
以这种方式使用 toString() 是不可靠的;对象可以通过定义 Symbol.toStringTag 属性来更改 Object.prototype.toString() 的行为,从而导致意外的结果。例如:
const myDate = new Date();
Object.prototype.toString.call(myDate); // [object Date]
myDate[Symbol.toStringTag] = "myDate";
Object.prototype.toString.call(myDate); // [object myDate]
Date.prototype[Symbol.toStringTag] = "prototype polluted";
Object.prototype.toString.call(new Date()); // [object prototype polluted]
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-object.prototype.tostring |
浏览器兼容性
加载中…