]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-new-object.md
377a682a9ef6b2e4723aba2b3f7483208235ec42
[pve-eslint.git] / eslint / docs / src / rules / no-new-object.md
1 ---
2 title: no-new-object
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-array-constructor
7 - no-new-wrappers
8 ---
9
10
11 The `Object` constructor is used to create new generic objects in JavaScript, such as:
12
13 ```js
14 var myObject = new Object();
15 ```
16
17 However, this is no different from using the more concise object literal syntax:
18
19 ```js
20 var myObject = {};
21 ```
22
23 For this reason, many prefer to always use the object literal syntax and never use the `Object` constructor.
24
25 While there are no performance differences between the two approaches, the byte savings and conciseness of the object literal form is what has made it the de facto way of creating new objects.
26
27 ## Rule Details
28
29 This rule disallows `Object` constructors.
30
31 Examples of **incorrect** code for this rule:
32
33 ::: incorrect
34
35 ```js
36 /*eslint no-new-object: "error"*/
37
38 var myObject = new Object();
39
40 new Object();
41 ```
42
43 :::
44
45 Examples of **correct** code for this rule:
46
47 ::: correct
48
49 ```js
50 /*eslint no-new-object: "error"*/
51
52 var myObject = new CustomObject();
53
54 var myObject = {};
55
56 var Object = function Object() {};
57 new Object();
58 ```
59
60 :::
61
62 ## When Not To Use It
63
64 If you wish to allow the use of the `Object` constructor, you can safely turn this rule off.