Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential error in chapter 6 - Symbols #459

Open
ronen-e opened this issue Mar 17, 2021 · 0 comments
Open

Potential error in chapter 6 - Symbols #459

ronen-e opened this issue Mar 17, 2021 · 0 comments

Comments

@ronen-e
Copy link
Contributor

ronen-e commented Mar 17, 2021

Where
Chapter 6 - Symbols, under Creating Symbols

Problem

Because symbols are primitive values, calling new Symbol() throws an error when called.

Reason
The cause of the error is not due to symbols being primitive values. Otherwise calling

new Boolean();

or

new Number();

would also throw an exception.

I did not find any record explaining the reason for this specification for throwing a TypeError when trying to explicitly call the Symbol constructor to get a Symbol object, but I believe it goes along the same lines as throwing a TypeError when trying to coerce a symbol implicitly to a string or number

const sym = Symbol();
'' + sym // TypeError
2 * sym // TypeError

The specification goes a long way towards making sure that symbols remain symbols and to prevent using them as a different type by accident.

According to Axel Rauschmayer the reason is most likely preventing accidental and unintentional type conversions which would lead to errors, mainly due to the role symbols play as unique property keys.
https://exploringjs.com/es6/ch_symbols.html#sec_converting-symbols-to-primitives

Solution 1

calling new Symbol() throws an error when called.

Solution 2

In order to prevent creating symbol objects instead of symbol primitive values, calling new Symbol() throws an error when called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant