60 lines
2.2 KiB
Plaintext

== Why is this an issue?
Using the `new` operator with `Symbol` and `BigInt` will throw a `TypeError` because they are not intended to be used as constructors. `Symbol` and `BigInt` are primitive types in JavaScript and should be used as such.
This is different from the other primitive types, such as string, number or boolean, where it was possible to call global `String` or `Number` as functions that return primitive types, but also use them as constructors with the `new` operator to create wrapper objects. This confusing double behavior is not implemented for `Symbol` and `BigInt` types that were introduced later in the language.
This behavior would be especially problematic for symbols that have reference identity and already behave like objects in some way. For example, they are garbage collectable and therefore can be used as keys in WeakMap and WeakSet objects.
[source,javascript,diff-id=1,diff-type=noncompliant]
----
let foo = new Symbol('abc'); // Noncompliant: TypeError: Symbol is not a constructor
let bar = new BigInt(123); // Noncompliant: TypeError: BigInt is not a constructor
----
To fix the code remove the `new` operator.
[source,javascript,diff-id=1,diff-type=compliant]
----
let foo = Symbol('abc');
let bar = BigInt(123);
----
For the `BigInt` type to be recognized correctly, the environment should be `es2020` or higher.
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol[Symbol]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt[BigInt]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new[new operator]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this "new" operator.
=== Highlighting
Primary: "new"
Secondary: "Symbol"
'''
== Comments And Links
(visible only on this page)
=== on 19 Jan 2017, 13:00:25 Elena Vilchik wrote:
\[~jeanchristophe.collet] Could you add to the description couple of words on what is ``++Symbol++`` is? thanks!
endif::env-github,rspecator-view[]