Throwing when symbol *wrappers* are converted to primitives

Claude Pache claude.pache at gmail.com
Sat Dec 27 00:49:49 PST 2014


Here is a more complete answer. In fact, the behaviour should be different in each your two cases. More inline.

> Le 27 déc. 2014 à 08:17, Axel Rauschmayer <axel at rauschma.de> a écrit :
> 
> This is current V8 behavior:
> 
> ```
> > let obj = {};
> > obj[Object(Symbol())] = true;
> TypeError: Cannot convert object to primitive value

That should convert back the wrapper object into the wrapped symbol (using ToPrimitive).
The rationale is that you obtained the same behaviour when you used Object("some string") as property key.
For how it works technically, see in that order:

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-topropertykey
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-toprimitive
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-symbol.prototype-@@toprimitive

> 
> > '' + Object(Symbol())
> TypeError: Cannot convert a Symbol wrapper object to a primitive value
> ```
> 

That should attempt to convert Object(Symbol()) to a string, and that should throw a TypeError. If I recall correctly (and I might not), the order of operations is:

1. apply ToPrimitive on the second operand, which produces the wrapped symbol;
2. decide a string is needed, apply ToString() on the symbol produced in step 1, which throws a TypeError.

Anyway, whenever `+` represents addition of numbers or concatenation of strings, that would throw. See:

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tostring

and

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-tonumber

—Claude



More information about the es-discuss mailing list