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 |
瀏覽器相容性
載入中…