What are Symbols? Objects? New primitive type?

Brandon Benvie bbenvie at mozilla.com
Fri Apr 12 15:12:45 PDT 2013

Based on recent discussions, it seems that there's some confusion about 
what exactly Symbols are. So far I've seen three different alternatives:

1.) Exotic objects that conform to the most recent ES6 spec, but with a 
special typeof .That is, they have special operations for all the 
internal methods. These internal methods make them act as stateless, 
immutable, prototypeless objects such that they can be treated like 
primitives. The problem here is introducing new typeof semantics, where 
something that otherwise acts like an object is not typeof === "object".

2.) A new type of primitive that have no object wrapper version, thus 
`ToObject(symbol)` throws. This means either they have to be falsey or a 
new situation arises where `if (val != null) val.prop` throws (ignoring 
accessors and proxies).

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.

A relevant gist here demonstrates test cases for option #1. I'll see if 
I can add comparable sets of tests for the other two options. [1]


More information about the es-discuss mailing list