There is a .toString()
on Symbol
in ES6 which returns the string representation of Symbol
, but wondering why '' + Symbol()
doesn't work (run this expression throws out TypeError
which I don't expect)? Is the latter just calling .toString()
on a new Symbol
and append (+
) it to empty string?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString.
No actually, Symbols cannot be implicitly cast to strings, or numbers, although interestingly enough you can implicitly cast them to a boolean.
MDN actually has a section on some of these pitfalls:
This behavior is documented in the spec under the abstract
ToString
operation:And similarly for abstract
ToNumber
operation:To cast a
Symbol
to a string without aTypeError
, you must use either thetoString
method, orString()
.