]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
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 | ||
609c276f | 115 | Examples 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 | |
120 | var friend = new person.acquaintance(); | |
609c276f | 121 | |
eb39fafa DC |
122 | var bestFriend = new person.friend(); |
123 | ``` | |
124 | ||
609c276f TL |
125 | Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "\\.bar$" }` option: |
126 | ||
127 | ```js | |
128 | /*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/ | |
129 | ||
130 | var friend = new person.bar(); | |
131 | ``` | |
132 | ||
eb39fafa DC |
133 | ### capIsNewExceptions |
134 | ||
135 | Examples of additional **correct** code for this rule with the `{ "capIsNewExceptions": ["Person"] }` option: | |
136 | ||
137 | ```js | |
138 | /*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/ | |
139 | ||
140 | function foo(arg) { | |
141 | return Person(arg); | |
142 | } | |
143 | ``` | |
144 | ||
145 | ### capIsNewExceptionPattern | |
146 | ||
609c276f | 147 | Examples 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 | |
152 | var friend = person.Acquaintance(); | |
153 | var bestFriend = person.Friend(); | |
154 | ``` | |
155 | ||
609c276f TL |
156 | Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "\\.Bar$" }` option: |
157 | ||
158 | ```js | |
159 | /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/ | |
160 | ||
161 | foo.Bar(); | |
162 | ``` | |
163 | ||
164 | Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^Foo" }` option: | |
165 | ||
166 | ```js | |
167 | /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/ | |
168 | ||
169 | var x = Foo(42); | |
170 | ||
171 | var y = Foobar(42); | |
172 | ||
173 | var z = Foo.Bar(42); | |
174 | ``` | |
175 | ||
eb39fafa DC |
176 | ### properties |
177 | ||
178 | Examples of **incorrect** code for this rule with the default `{ "properties": true }` option: | |
179 | ||
180 | ```js | |
181 | /*eslint new-cap: ["error", { "properties": true }]*/ | |
182 | ||
183 | var friend = new person.acquaintance(); | |
184 | ``` | |
185 | ||
186 | Examples of **correct** code for this rule with the default `{ "properties": true }` option: | |
187 | ||
188 | ```js | |
189 | /*eslint new-cap: ["error", { "properties": true }]*/ | |
190 | ||
191 | var friend = new person.Acquaintance(); | |
192 | ``` | |
193 | ||
194 | Examples of **correct** code for this rule with the `{ "properties": false }` option: | |
195 | ||
196 | ```js | |
197 | /*eslint new-cap: ["error", { "properties": false }]*/ | |
198 | ||
199 | var friend = new person.acquaintance(); | |
200 | ``` | |
201 | ||
202 | ## When Not To Use It | |
203 | ||
204 | 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. |