You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
newBoolean();
or
newNumber();
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
Where
Chapter 6 - Symbols, under Creating Symbols
Problem
Reason
The cause of the error is not due to symbols being primitive values. Otherwise calling
or
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
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
Solution 2
The text was updated successfully, but these errors were encountered: