]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-new.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-new.md
1 ---
2 title: no-new
3 rule_type: suggestion
4 ---
5
6
7 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:
8
9 ```js
10 var person = new Person();
11 ```
12
13 It's less common to use `new` and not store the result, such as:
14
15 ```js
16 new Person();
17 ```
18
19 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.
20
21 ## Rule Details
22
23 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.
24
25 Examples of **incorrect** code for this rule:
26
27 ::: incorrect
28
29 ```js
30 /*eslint no-new: "error"*/
31
32 new Thing();
33 ```
34
35 :::
36
37 Examples of **correct** code for this rule:
38
39 ::: correct
40
41 ```js
42 /*eslint no-new: "error"*/
43
44 var thing = new Thing();
45
46 Foo();
47 ```
48
49 :::