为什么隐式符号到字符串转换会导致 JavaScript 中的 TypeError?
Why does implicit Symbol to String conversion' lead to TypeError in JavaScript?
在ES6中Symbol
上有一个.toString()
,其中returns是Symbol
的字符串表示,但想知道为什么'' + Symbol()
不起作用( 运行 这个表达式会抛出 TypeError
,我没想到)?后者只是在新的 Symbol
上调用 .toString()
并将其附加 (+
) 到空字符串吗?
来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString.
The Symbol object overrides the toString method of the Object object;
it does not inherit Object.prototype.toString(). For Symbol objects,
the toString method returns a string representation of the object.
Is the latter just calling .toString()
on a new Symbol
and append (+
) it to empty string?
实际上,符号不能隐式转换为字符串或数字,尽管有趣的是您可以将它们隐式转换为布尔值。
MDN actually has a section 关于其中一些陷阱:
Symbol type conversions
Some things to note when working with type conversion of symbols.
- When trying to convert a symbol to a number, a
TypeError
will be thrown (e.g. +sym
or sym | 0
).
- When using loose equality,
Object(sym) == sym
returns true.
Symbol("foo") + "bar"
throws a TypeError
(can't convert symbol to string). This prevents you from silently creating a new string property name from a symbol, for example.
- The "safer"
String(sym)
conversion works like a call to Symbol.prototype.toString()
with symbols, but note that new String(sym)
will throw.
此行为记录在 abstract ToString
operation 下的规范中:
Argument Type: Symbol
Result: Throw a TypeError
exception.
abstract ToNumber
operation 也类似:
Argument Type: Symbol
Result: Throw a TypeError
exception.
要将 Symbol
转换为不带 TypeError
的字符串,您必须使用 toString
方法或 String()
.
在ES6中Symbol
上有一个.toString()
,其中returns是Symbol
的字符串表示,但想知道为什么'' + Symbol()
不起作用( 运行 这个表达式会抛出 TypeError
,我没想到)?后者只是在新的 Symbol
上调用 .toString()
并将其附加 (+
) 到空字符串吗?
来自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString.
The Symbol object overrides the toString method of the Object object; it does not inherit Object.prototype.toString(). For Symbol objects, the toString method returns a string representation of the object.
Is the latter just calling
.toString()
on a newSymbol
and append (+
) it to empty string?
实际上,符号不能隐式转换为字符串或数字,尽管有趣的是您可以将它们隐式转换为布尔值。
MDN actually has a section 关于其中一些陷阱:
Symbol type conversions
Some things to note when working with type conversion of symbols.
- When trying to convert a symbol to a number, a
TypeError
will be thrown (e.g.+sym
orsym | 0
).- When using loose equality,
Object(sym) == sym
returnstrue.
Symbol("foo") + "bar"
throws aTypeError
(can't convert symbol to string). This prevents you from silently creating a new string property name from a symbol, for example.- The "safer"
String(sym)
conversion works like a call toSymbol.prototype.toString()
with symbols, but note thatnew String(sym)
will throw.
此行为记录在 abstract ToString
operation 下的规范中:
Argument Type: Symbol
Result: Throw a
TypeError
exception.
abstract ToNumber
operation 也类似:
Argument Type: Symbol
Result: Throw a
TypeError
exception.
要将 Symbol
转换为不带 TypeError
的字符串,您必须使用 toString
方法或 String()
.