]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/new-cap.md
first commit
[pve-eslint.git] / eslint / docs / rules / new-cap.md
1 # require constructor names to begin with a capital letter (new-cap)
2
3 The `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
6 var friend = new Person();
7 ```
8
9 ## Rule Details
10
11 This 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
24 Examples of **correct** code for this rule:
25
26 ```js
27 /*eslint new-cap: "error"*/
28
29 function foo(arg) {
30 return Boolean(arg);
31 }
32 ```
33
34 ## Options
35
36 This 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
51 Examples of **incorrect** code for this rule with the default `{ "newIsCap": true }` option:
52
53 ```js
54 /*eslint new-cap: ["error", { "newIsCap": true }]*/
55
56 var friend = new person();
57 ```
58
59 Examples of **correct** code for this rule with the default `{ "newIsCap": true }` option:
60
61 ```js
62 /*eslint new-cap: ["error", { "newIsCap": true }]*/
63
64 var friend = new Person();
65 ```
66
67 Examples of **correct** code for this rule with the `{ "newIsCap": false }` option:
68
69 ```js
70 /*eslint new-cap: ["error", { "newIsCap": false }]*/
71
72 var friend = new person();
73 ```
74
75 ### capIsNew
76
77 Examples of **incorrect** code for this rule with the default `{ "capIsNew": true }` option:
78
79 ```js
80 /*eslint new-cap: ["error", { "capIsNew": true }]*/
81
82 var colleague = Person();
83 ```
84
85 Examples of **correct** code for this rule with the default `{ "capIsNew": true }` option:
86
87 ```js
88 /*eslint new-cap: ["error", { "capIsNew": true }]*/
89
90 var colleague = new Person();
91 ```
92
93 Examples of **correct** code for this rule with the `{ "capIsNew": false }` option:
94
95 ```js
96 /*eslint new-cap: ["error", { "capIsNew": false }]*/
97
98 var colleague = Person();
99 ```
100
101 ### newIsCapExceptions
102
103 Examples of additional **correct** code for this rule with the `{ "newIsCapExceptions": ["events"] }` option:
104
105 ```js
106 /*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
107
108 var events = require('events');
109
110 var emitter = new events();
111 ```
112
113 ### newIsCapExceptionPattern
114
115 Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "^person\.." }` option:
116
117 ```js
118 /*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\.." }]*/
119
120 var friend = new person.acquaintance();
121 var bestFriend = new person.friend();
122 ```
123
124 ### capIsNewExceptions
125
126 Examples of additional **correct** code for this rule with the `{ "capIsNewExceptions": ["Person"] }` option:
127
128 ```js
129 /*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
130
131 function foo(arg) {
132 return Person(arg);
133 }
134 ```
135
136 ### capIsNewExceptionPattern
137
138 Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^Person\.." }` option:
139
140 ```js
141 /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Person\.." }]*/
142
143 var friend = person.Acquaintance();
144 var bestFriend = person.Friend();
145 ```
146
147 ### properties
148
149 Examples of **incorrect** code for this rule with the default `{ "properties": true }` option:
150
151 ```js
152 /*eslint new-cap: ["error", { "properties": true }]*/
153
154 var friend = new person.acquaintance();
155 ```
156
157 Examples of **correct** code for this rule with the default `{ "properties": true }` option:
158
159 ```js
160 /*eslint new-cap: ["error", { "properties": true }]*/
161
162 var friend = new person.Acquaintance();
163 ```
164
165 Examples of **correct** code for this rule with the `{ "properties": false }` option:
166
167 ```js
168 /*eslint new-cap: ["error", { "properties": false }]*/
169
170 var friend = new person.acquaintance();
171 ```
172
173 ## When Not To Use It
174
175 If 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.