Convert JavaScript Symbol to String?

2019-04-22 20:37发布

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?

2条回答
家丑人穷心不美
2楼-- · 2019-04-22 21:10

From 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.

查看更多
Anthone
3楼-- · 2019-04-22 21:12

Is the latter just calling .toString() on a new Symbol and append (+) it to empty string?

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:

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.

This behavior is documented in the spec under the abstract ToString operation:

Argument Type: Symbol

Result: Throw a TypeError exception.

And similarly for abstract ToNumber operation:

Argument Type: Symbol

Result: Throw a TypeError exception.

To cast a Symbol to a string without a TypeError, you must use either the toString method, or String().

查看更多
登录 后发表回答