new-cap
要求构造函数名称以大写字母开头
JavaScript 中的 new
运算符创建特定类型对象的新实例。这种类型的对象由构造函数表示。由于构造函数只是常规函数,唯一的定义特性是 new
被用作调用的一部分。原生 JavaScript 函数以大写字母开头,以区分用作构造函数的函数和不用作构造函数的函数。许多风格指南建议遵循此模式,以便更轻松地确定将哪些函数用作构造函数。
¥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.
var friend = new Person();
规则详情
¥Rule Details
此规则要求构造函数名称以大写字母开头。某些内置标识符不受此规则的约束。这些标识符是:
¥This rule requires constructor names to begin with a capital letter. Certain built-in identifiers are exempt from this rule. These identifiers are:
-
Array
-
Boolean
-
Date
-
Error
-
Function
-
Number
-
Object
-
RegExp
-
String
-
Symbol
-
BigInt
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint new-cap: "error"*/
function foo(arg) {
return Boolean(arg);
}
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"newIsCap": true
(默认)要求使用大写开头的函数调用所有new
运算符。¥
"newIsCap": true
(default) requires allnew
operators to be called with uppercase-started functions. -
"newIsCap": false
允许使用小写开头或大写开头的函数调用new
运算符。¥
"newIsCap": false
allowsnew
operators to be called with lowercase-started or uppercase-started functions. -
"capIsNew": true
(默认)要求使用new
运算符调用所有大写开头的函数。¥
"capIsNew": true
(default) requires all uppercase-started functions to be called withnew
operators. -
"capIsNew": false
允许在没有new
运算符的情况下调用以大写开头的函数。¥
"capIsNew": false
allows uppercase-started functions to be called withoutnew
operators. -
"newIsCapExceptions"
允许使用new
运算符调用指定的小写开头的函数名称。¥
"newIsCapExceptions"
allows specified lowercase-started function names to be called with thenew
operator. -
"newIsCapExceptionPattern"
允许使用new
运算符调用与指定正则表达式模式匹配的任何小写开头的函数名称。¥
"newIsCapExceptionPattern"
allows any lowercase-started function names that match the specified regex pattern to be called with thenew
operator. -
"capIsNewExceptions"
允许在不使用new
运算符的情况下调用指定的大写开头函数名称。¥
"capIsNewExceptions"
allows specified uppercase-started function names to be called without thenew
operator. -
"capIsNewExceptionPattern"
允许在不使用new
运算符的情况下调用与指定正则表达式模式匹配的任何以大写开头的函数名称。¥
"capIsNewExceptionPattern"
allows any uppercase-started function names that match the specified regex pattern to be called without thenew
operator. -
"properties": true
(默认)启用对对象属性的检查¥
"properties": true
(default) enables checks on object properties -
"properties": false
禁用对对象属性的检查¥
"properties": false
disables checks on object properties
newIsCap
使用默认 { "newIsCap": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "newIsCap": true }
option:
/*eslint new-cap: ["error", { "newIsCap": true }]*/
var friend = new person();
使用默认 { "newIsCap": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "newIsCap": true }
option:
/*eslint new-cap: ["error", { "newIsCap": true }]*/
var friend = new Person();
使用 { "newIsCap": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "newIsCap": false }
option:
/*eslint new-cap: ["error", { "newIsCap": false }]*/
var friend = new person();
capIsNew
使用默认 { "capIsNew": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "capIsNew": true }
option:
/*eslint new-cap: ["error", { "capIsNew": true }]*/
var colleague = Person();
使用默认 { "capIsNew": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "capIsNew": true }
option:
/*eslint new-cap: ["error", { "capIsNew": true }]*/
var colleague = new Person();
使用 { "capIsNew": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "capIsNew": false }
option:
/*eslint new-cap: ["error", { "capIsNew": false }]*/
var colleague = Person();
newIsCapExceptions
使用 { "newIsCapExceptions": ["events"] }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "newIsCapExceptions": ["events"] }
option:
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
var events = require('events');
var emitter = new events();
newIsCapExceptionPattern
使用 { "newIsCapExceptionPattern": "^person\\.." }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "newIsCapExceptionPattern": "^person\\.." }
option:
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/
var friend = new person.acquaintance();
var bestFriend = new person.friend();
使用 { "newIsCapExceptionPattern": "\\.bar$" }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "newIsCapExceptionPattern": "\\.bar$" }
option:
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/
var friend = new person.bar();
capIsNewExceptions
使用 { "capIsNewExceptions": ["Person"] }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "capIsNewExceptions": ["Person"] }
option:
/*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
function foo(arg) {
return Person(arg);
}
capIsNewExceptionPattern
使用 { "capIsNewExceptionPattern": "^person\\.." }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "capIsNewExceptionPattern": "^person\\.." }
option:
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/
var friend = person.Acquaintance();
var bestFriend = person.Friend();
使用 { "capIsNewExceptionPattern": "\\.Bar$" }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "capIsNewExceptionPattern": "\\.Bar$" }
option:
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/
foo.Bar();
使用 { "capIsNewExceptionPattern": "^Foo" }
选项的此规则的附加正确代码示例:
¥Examples of additional correct code for this rule with the { "capIsNewExceptionPattern": "^Foo" }
option:
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/
var x = Foo(42);
var y = Foobar(42);
var z = Foo.Bar(42);
properties
使用默认 { "properties": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "properties": true }
option:
/*eslint new-cap: ["error", { "properties": true }]*/
var friend = new person.acquaintance();
使用默认 { "properties": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "properties": true }
option:
/*eslint new-cap: ["error", { "properties": true }]*/
var friend = new person.Acquaintance();
使用 { "properties": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "properties": false }
option:
/*eslint new-cap: ["error", { "properties": false }]*/
var friend = new person.acquaintance();
何时不使用
¥When Not To Use It
如果你的约定不需要构造函数使用大写字母,或者不需要大写函数仅用作构造函数,请关闭此规则。
¥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.
版本
此规则是在 ESLint v0.0.3-0 中引入。