]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-new-native-nonconstructor.md
c9b75679a1ef1f7c641444a6aca4279f79c3d433
[pve-eslint.git] / eslint / docs / src / rules / no-new-native-nonconstructor.md
1 ---
2 title: no-new-native-nonconstructor
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - no-obj-calls
7 further_reading:
8 - https://tc39.es/ecma262/#sec-symbol-constructor
9 - https://tc39.es/ecma262/#sec-bigint-constructor
10 ---
11
12
13
14 It is a convention in JavaScript that global variables beginning with an uppercase letter typically represent classes that can be instantiated using the `new` operator, such as `new Array` and `new Map`. Confusingly, JavaScript also provides some global variables that begin with an uppercase letter that cannot be called using the `new` operator and will throw an error if you attempt to do so. These are typically functions that are related to data types and are easy to mistake for classes. Consider the following example:
15
16 ```js
17 // throws a TypeError
18 let foo = new Symbol("foo");
19
20 // throws a TypeError
21 let result = new BigInt(9007199254740991);
22 ```
23
24 Both `new Symbol` and `new BigInt` throw a type error because they are functions and not classes. It is easy to make this mistake by assuming the uppercase letters indicate classes.
25
26 ## Rule Details
27
28 This rule is aimed at preventing the accidental calling of native JavaScript global functions with the `new` operator. These functions are:
29
30 * `Symbol`
31 * `BigInt`
32
33 ## Examples
34
35 Examples of **incorrect** code for this rule:
36
37 ::: incorrect
38
39 ```js
40 /*eslint no-new-native-nonconstructor: "error"*/
41 /*eslint-env es2022*/
42
43 var foo = new Symbol('foo');
44 var bar = new BigInt(9007199254740991);
45 ```
46
47 :::
48
49 Examples of **correct** code for this rule:
50
51 ::: correct
52
53 ```js
54 /*eslint no-new-native-nonconstructor: "error"*/
55 /*eslint-env es2022*/
56
57 var foo = Symbol('foo');
58 var bar = BigInt(9007199254740991);
59
60 // Ignores shadowed Symbol.
61 function baz(Symbol) {
62 const qux = new Symbol("baz");
63 }
64 function quux(BigInt) {
65 const corge = new BigInt(9007199254740991);
66 }
67
68 ```
69
70 :::
71
72 ## When Not To Use It
73
74 This rule should not be used in ES3/5 environments.