Symbol.toStringTag well-known symbol is a string valued property that is used in the creation of the default string description of an object. It is accessed internally by the Object.prototype.toString() 方法。

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

特性属性在 Symbol.toStringTag
可写 no
可枚举 no
可配置 no

范例

Default tags

Object.prototype.toString.call('foo');     // "[object String]"
Object.prototype.toString.call([1, 2]);    // "[object Array]"
Object.prototype.toString.call(3);         // "[object Number]"
Object.prototype.toString.call(true);      // "[object Boolean]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(null);      // "[object Null]"
// ... and more
				

Built-in toStringTag symbols

Object.prototype.toString.call(new Map());       // "[object Map]"
Object.prototype.toString.call(function* () {}); // "[object GeneratorFunction]"
Object.prototype.toString.call(Promise.resolve()); // "[object Promise]"
// ... and more
				

Custom classes default to object tag

When creating your own class, JavaScript defaults to the "Object" tag:

class ValidatorClass {}
Object.prototype.toString.call(new ValidatorClass()); // "[object Object]"
				

Custom tag with toStringTag

Now, with the help of toStringTag , you are able to set your own custom tag:

class ValidatorClass {
  get [Symbol.toStringTag]() {
    return 'Validator';
  }
}
Object.prototype.toString.call(new ValidatorClass()); // "[object Validator]"
				

toStringTag available on all DOM prototype objects

Due to a WebIDL spec change in mid-2020, browsers are adding a Symbol.toStringTag property to all DOM prototype objects. For example, to acccess the Symbol.toStringTag property on HTMLButtonElement :

let test = document.createElement('button');
test.toString(); // Returns [object HTMLButtonElement]
test[Symbol.toStringTag];  // Returns HTMLButtonElement
				

规范

规范
ECMAScript (ECMA-262)
The definition of 'Symbol.toStringTag' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
toStringTag Chrome 49 Edge 15 Firefox 51 IE No Opera 36 Safari 10 WebView Android 49 Chrome Android 49 Firefox Android 51 Opera Android 36 Safari iOS 10 Samsung Internet Android 5.0 nodejs 6.0.0
6.0.0
4.0.0 Disabled
Disabled From version 4.0.0: this feature is behind the --harmony runtime flag.
toStringTag available on all DOM prototype objects Chrome 50 Edge 79 Firefox 78 IE No Opera 37 Safari 14 WebView Android 50 Chrome Android 50 Firefox Android No Opera Android 37 Safari iOS 14 Samsung Internet Android 5.0 nodejs No

图例

完整支持

完整支持

不支持

不支持

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: