]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/new-cap.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / new-cap.md
CommitLineData
eb39fafa
DC
1# require constructor names to begin with a capital letter (new-cap)
2
3The `new` operator in JavaScript creates a new instance of a particular type of object. That type of object is represented by a constructor function. Since constructor functions are just regular functions, the only defining characteristic is that `new` is being used as part of the call. Native JavaScript functions begin with an uppercase letter to distinguish those functions that are to be used as constructors from functions that are not. Many style guides recommend following this pattern to more easily determine which functions are to be used as constructors.
4
5```js
6var friend = new Person();
7```
8
9## Rule Details
10
11This rule requires constructor names to begin with a capital letter. Certain built-in identifiers are exempt from this rule. These identifiers are:
12
13* `Array`
14* `Boolean`
15* `Date`
16* `Error`
17* `Function`
18* `Number`
19* `Object`
20* `RegExp`
21* `String`
22* `Symbol`
23
24Examples of **correct** code for this rule:
25
26```js
27/*eslint new-cap: "error"*/
28
29function foo(arg) {
30 return Boolean(arg);
31}
32```
33
34## Options
35
36This rule has an object option:
37
38* `"newIsCap": true` (default) requires all `new` operators to be called with uppercase-started functions.
39* `"newIsCap": false` allows `new` operators to be called with lowercase-started or uppercase-started functions.
40* `"capIsNew": true` (default) requires all uppercase-started functions to be called with `new` operators.
41* `"capIsNew": false` allows uppercase-started functions to be called without `new` operators.
42* `"newIsCapExceptions"` allows specified lowercase-started function names to be called with the `new` operator.
43* `"newIsCapExceptionPattern"` allows any lowercase-started function names that match the specified regex pattern to be called with the `new` operator.
44* `"capIsNewExceptions"` allows specified uppercase-started function names to be called without the `new` operator.
45* `"capIsNewExceptionPattern"` allows any uppercase-started function names that match the specified regex pattern to be called without the `new` operator.
46* `"properties": true` (default) enables checks on object properties
47* `"properties": false` disables checks on object properties
48
49### newIsCap
50
51Examples of **incorrect** code for this rule with the default `{ "newIsCap": true }` option:
52
53```js
54/*eslint new-cap: ["error", { "newIsCap": true }]*/
55
56var friend = new person();
57```
58
59Examples of **correct** code for this rule with the default `{ "newIsCap": true }` option:
60
61```js
62/*eslint new-cap: ["error", { "newIsCap": true }]*/
63
64var friend = new Person();
65```
66
67Examples of **correct** code for this rule with the `{ "newIsCap": false }` option:
68
69```js
70/*eslint new-cap: ["error", { "newIsCap": false }]*/
71
72var friend = new person();
73```
74
75### capIsNew
76
77Examples of **incorrect** code for this rule with the default `{ "capIsNew": true }` option:
78
79```js
80/*eslint new-cap: ["error", { "capIsNew": true }]*/
81
82var colleague = Person();
83```
84
85Examples of **correct** code for this rule with the default `{ "capIsNew": true }` option:
86
87```js
88/*eslint new-cap: ["error", { "capIsNew": true }]*/
89
90var colleague = new Person();
91```
92
93Examples of **correct** code for this rule with the `{ "capIsNew": false }` option:
94
95```js
96/*eslint new-cap: ["error", { "capIsNew": false }]*/
97
98var colleague = Person();
99```
100
101### newIsCapExceptions
102
103Examples of additional **correct** code for this rule with the `{ "newIsCapExceptions": ["events"] }` option:
104
105```js
106/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
107
108var events = require('events');
109
110var emitter = new events();
111```
112
113### newIsCapExceptionPattern
114
609c276f 115Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "^person\\.." }` option:
eb39fafa
DC
116
117```js
609c276f 118/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/
eb39fafa
DC
119
120var friend = new person.acquaintance();
609c276f 121
eb39fafa
DC
122var bestFriend = new person.friend();
123```
124
609c276f
TL
125Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "\\.bar$" }` option:
126
127```js
128/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/
129
130var friend = new person.bar();
131```
132
eb39fafa
DC
133### capIsNewExceptions
134
135Examples of additional **correct** code for this rule with the `{ "capIsNewExceptions": ["Person"] }` option:
136
137```js
138/*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
139
140function foo(arg) {
141 return Person(arg);
142}
143```
144
145### capIsNewExceptionPattern
146
609c276f 147Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^person\\.." }` option:
eb39fafa
DC
148
149```js
609c276f 150/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/
eb39fafa
DC
151
152var friend = person.Acquaintance();
153var bestFriend = person.Friend();
154```
155
609c276f
TL
156Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "\\.Bar$" }` option:
157
158```js
159/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/
160
161foo.Bar();
162```
163
164Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^Foo" }` option:
165
166```js
167/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/
168
169var x = Foo(42);
170
171var y = Foobar(42);
172
173var z = Foo.Bar(42);
174```
175
eb39fafa
DC
176### properties
177
178Examples of **incorrect** code for this rule with the default `{ "properties": true }` option:
179
180```js
181/*eslint new-cap: ["error", { "properties": true }]*/
182
183var friend = new person.acquaintance();
184```
185
186Examples of **correct** code for this rule with the default `{ "properties": true }` option:
187
188```js
189/*eslint new-cap: ["error", { "properties": true }]*/
190
191var friend = new person.Acquaintance();
192```
193
194Examples of **correct** code for this rule with the `{ "properties": false }` option:
195
196```js
197/*eslint new-cap: ["error", { "properties": false }]*/
198
199var friend = new person.acquaintance();
200```
201
202## When Not To Use It
203
204If you have conventions that don't require an uppercase letter for constructors, or don't require capitalized functions be only used as constructors, turn this rule off.