Object.prototype.toString()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

toString() 方法是 Object 例項的一個方法,它返回一個表示該物件的字串。此方法旨在由派生物件進行重寫,以實現自定義的 型別轉換 邏輯。

試一試

const map = new Map();

console.log(map.toString());
// Expected output: "[object Map]"

語法

js
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]() 方法,它允許對轉換過程進行更精細地控制,並且在任何型別轉換中始終優先於 valueOftoString

要使用基礎的 Object.prototype.toString() 方法處理一個已被重寫的物件(或者在 nullundefined 上呼叫它),你需要對其呼叫 Function.prototype.call()Function.prototype.apply(),並將你想要檢查的物件作為第一個引數傳遞(稱為 thisArg)。

js
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 使用。許多內建物件,包括 MapSymbol,都有一個 Symbol.toStringTag。一些早於 ES6 的物件沒有 Symbol.toStringTag,但仍然有一個特殊的標籤。它們包括(標籤與下面給出的型別名稱相同):

arguments 物件返回 "[object Arguments]"。其他所有物件,包括使用者定義的類,除非有自定義的 Symbol.toStringTag,否則將返回 "[object Object]"

nullundefined 上呼叫 Object.prototype.toString() 分別返回 [object Null][object Undefined]

示例

為自定義物件重寫 toString

你可以建立一個函式來替代預設的 toString() 方法。你建立的 toString() 函式應該返回一個字串值。如果它返回一個物件,並且該方法在 型別轉換 期間被隱式呼叫,那麼它的結果將被忽略,而是使用相關方法 valueOf() 的值,或者如果這些方法都不返回原始值,則會丟擲 TypeError

以下程式碼定義了一個 Dog 類。

js
class Dog {
  constructor(name, breed, color, sex) {
    this.name = name;
    this.breed = breed;
    this.color = color;
    this.sex = sex;
  }
}

如果你顯式或隱式地在 Dog 例項上呼叫 toString() 方法,它將返回從 Object 繼承的預設值。

js
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");

theDog.toString(); // "[object Object]"
`${theDog}`; // "[object Object]"

以下程式碼重寫了預設的 toString() 方法。此方法生成一個包含物件的 namebreedcolorsex 的字串。

js
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() 方法。

js
const theDog = new Dog("Gabby", "Lab", "chocolate", "female");

`${theDog}`; // "Dog Gabby is a female chocolate Lab"

使用 toString() 檢測物件類

toString() 可與所有物件一起使用,(預設情況下)允許你獲取其類。

js
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() 的行為,從而導致意外的結果。例如:

js
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

瀏覽器相容性

另見