]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-new-object.md
first commit
[pve-eslint.git] / eslint / docs / rules / no-new-object.md
1 # disallow `Object` constructors (no-new-object)
2
3 The `Object` constructor is used to create new generic objects in JavaScript, such as:
4
5 ```js
6 var myObject = new Object();
7 ```
8
9 However, this is no different from using the more concise object literal syntax:
10
11 ```js
12 var myObject = {};
13 ```
14
15 For this reason, many prefer to always use the object literal syntax and never use the `Object` constructor.
16
17 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.
18
19 ## Rule Details
20
21 This rule disallows `Object` constructors.
22
23 Examples of **incorrect** code for this rule:
24
25 ```js
26 /*eslint no-new-object: "error"*/
27
28 var myObject = new Object();
29
30 var myObject = new Object;
31 ```
32
33 Examples of **correct** code for this rule:
34
35 ```js
36 /*eslint no-new-object: "error"*/
37
38 var myObject = new CustomObject();
39
40 var myObject = {};
41 ```
42
43 ## When Not To Use It
44
45 If you wish to allow the use of the `Object` constructor, you can safely turn this rule off.
46
47 ## Related Rules
48
49 * [no-array-constructor](no-array-constructor.md)
50 * [no-new-wrappers](no-new-wrappers.md)