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