1 # require constructor names to begin with a capital letter (new-cap)
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.
6 var friend = new Person();
11 This rule requires constructor names to begin with a capital letter. Certain built-in identifiers are exempt from this rule. These identifiers are:
24 Examples of **correct** code for this rule:
27 /*eslint new-cap: "error"*/
36 This rule has an object option:
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
51 Examples of **incorrect** code for this rule with the default `{ "newIsCap": true }` option:
54 /*eslint new-cap: ["error", { "newIsCap": true }]*/
56 var friend = new person();
59 Examples of **correct** code for this rule with the default `{ "newIsCap": true }` option:
62 /*eslint new-cap: ["error", { "newIsCap": true }]*/
64 var friend = new Person();
67 Examples of **correct** code for this rule with the `{ "newIsCap": false }` option:
70 /*eslint new-cap: ["error", { "newIsCap": false }]*/
72 var friend = new person();
77 Examples of **incorrect** code for this rule with the default `{ "capIsNew": true }` option:
80 /*eslint new-cap: ["error", { "capIsNew": true }]*/
82 var colleague = Person();
85 Examples of **correct** code for this rule with the default `{ "capIsNew": true }` option:
88 /*eslint new-cap: ["error", { "capIsNew": true }]*/
90 var colleague = new Person();
93 Examples of **correct** code for this rule with the `{ "capIsNew": false }` option:
96 /*eslint new-cap: ["error", { "capIsNew": false }]*/
98 var colleague = Person();
101 ### newIsCapExceptions
103 Examples of additional **correct** code for this rule with the `{ "newIsCapExceptions": ["events"] }` option:
106 /*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
108 var events = require('events');
110 var emitter = new events();
113 ### newIsCapExceptionPattern
115 Examples of additional **correct** code for this rule with the `{ "newIsCapExceptionPattern": "^person\.." }` option:
118 /*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\.." }]*/
120 var friend = new person.acquaintance();
121 var bestFriend = new person.friend();
124 ### capIsNewExceptions
126 Examples of additional **correct** code for this rule with the `{ "capIsNewExceptions": ["Person"] }` option:
129 /*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
136 ### capIsNewExceptionPattern
138 Examples of additional **correct** code for this rule with the `{ "capIsNewExceptionPattern": "^Person\.." }` option:
141 /*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Person\.." }]*/
143 var friend = person.Acquaintance();
144 var bestFriend = person.Friend();
149 Examples of **incorrect** code for this rule with the default `{ "properties": true }` option:
152 /*eslint new-cap: ["error", { "properties": true }]*/
154 var friend = new person.acquaintance();
157 Examples of **correct** code for this rule with the default `{ "properties": true }` option:
160 /*eslint new-cap: ["error", { "properties": true }]*/
162 var friend = new person.Acquaintance();
165 Examples of **correct** code for this rule with the `{ "properties": false }` option:
168 /*eslint new-cap: ["error", { "properties": false }]*/
170 var friend = new person.acquaintance();
173 ## When Not To Use It
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.