概述
toString()
方法返回当前 symbol 对象的字符串表示。
语法
symbol.toString();
描述
symbol 原始值不能转换为字符串
symbol 原始值不能转换为字符串,所以只能先转换成它的包装对象,再调用 toString()
方法:
Symbol("foo") + "bar"; // TypeError: Can't convert symbol to string Symbol("foo").toString() + "bar" // "Symbol(foo)bar",就相当于下面的: Object(Symbol("foo")).toString() + "bar" // "Symbol(foo)bar"
示例
Symbol("desc").toString(); // "Symbol(desc)" // well-known symbols Symbol.iterator.toString(); // "Symbol(Symbol.iterator) // global symbols Symbol.for("foo").toString() // "Symbol(foo)"
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) Symbol.prototype.toString |
Standard | Initial definition. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 33.0 (33.0) | 未实现 | 未实现 | 未实现 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 33.0 (33.0) | 未实现 | 未实现 | 未实现 |