]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-new.md
19ae097192d30df891f47bb764ac228abee32db8
[pve-eslint.git] / eslint / docs / rules / no-new.md
1 # Disallow new For Side Effects (no-new)
2
3 The goal of using `new` with a constructor is typically to create an object of a particular type and store that object in a variable, such as:
4
5 ```js
6 var person = new Person();
7 ```
8
9 It's less common to use `new` and not store the result, such as:
10
11 ```js
12 new Person();
13 ```
14
15 In this case, the created object is thrown away because its reference isn't stored anywhere, and in many cases, this means that the constructor should be replaced with a function that doesn't require `new` to be used.
16
17 ## Rule Details
18
19 This rule is aimed at maintaining consistency and convention by disallowing constructor calls using the `new` keyword that do not assign the resulting object to a variable.
20
21 Examples of **incorrect** code for this rule:
22
23 ```js
24 /*eslint no-new: "error"*/
25
26 new Thing();
27 ```
28
29 Examples of **correct** code for this rule:
30
31 ```js
32 /*eslint no-new: "error"*/
33
34 var thing = new Thing();
35
36 Thing();
37 ```