What are Symbols? Objects? New primitive type?

Andreas Rossberg rossberg at google.com
Sat Apr 13 00:31:57 PDT 2013

On 13 April 2013 00:12, Brandon Benvie <bbenvie at mozilla.com> wrote:
> 3.) A new type of primitive along with a new type of wrapper. In this case
> we use the String/Number/Boolean precedent where `Symbol()` and `new
> Symbol()` produce different kinds of results. The problem here is the
> confusion that comes with ToString/ToPropertyKey when called on a Symbol
> wrapper. What does `obj[new Symbol] = 5` do, for example? It allows footguns
> like `obj[key + '_ext']` to silently do the wrong thing.

That was the consensus at the last meeting, and it's already
implemented in V8. The decision included that
Symbol.prototype.toString is poisoned, i.e., any attempt to implicitly
convert a symbol or a wrapped symbol to a string will throw, including
both of your examples. So no footgun, which I agree is important.
(Object.prototype.toString is not on that path, however, so can treat
symbols separately.)

I'm a bit confused about some bits of Allen's comment on your Gist,
though, which don't line up with what I thought we decided on. In
particular, ToObject(symbol) should be perfectly fine and creates a
wrapper object. Similarly, Symbol()["foo"] auto-converts to a wrapper
object and returns undefined as usual, and new Symbol() directly
creates a wrapper object.


More information about the es-discuss mailing list