new-cap
要求建構子名稱以大寫字母開頭
JavaScript 中的 new
運算子會建立特定物件類型的新實例。該物件類型由建構子函式表示。由於建構子函式只是常規函式,因此唯一的決定性特徵是 new
被用作呼叫的一部分。原生 JavaScript 函式以大寫字母開頭,以區分那些要用作建構子的函式與那些不用作建構子的函式。許多風格指南建議遵循此模式,以便更輕鬆地確定哪些函式要用作建構子。
const friend = new Person();
規則詳情
此規則要求建構子名稱以大寫字母開頭。某些內建識別符號可豁免於此規則。這些識別符號是
Array
Boolean
Date
Error
Function
Number
Object
RegExp
String
Symbol
BigInt
此規則的 正確 程式碼範例
/*eslint new-cap: "error"*/
function foo(arg) {
return Boolean(arg);
}
選項
此規則有一個物件選項
"newIsCap": true
(預設)要求所有new
運算子都使用大寫開頭的函式呼叫。"newIsCap": false
允許new
運算子使用小寫開頭或大寫開頭的函式呼叫。"capIsNew": true
(預設)要求所有大寫開頭的函式都使用new
運算子呼叫。"capIsNew": false
允許大寫開頭的函式在沒有new
運算子的情況下呼叫。"newIsCapExceptions"
允許指定的小寫開頭函式名稱使用new
運算子呼叫。"newIsCapExceptionPattern"
允許任何符合指定 regex 模式的小寫開頭函式名稱使用new
運算子呼叫。"capIsNewExceptions"
允許指定的大寫開頭函式名稱在沒有new
運算子的情況下呼叫。"capIsNewExceptionPattern"
允許任何符合指定 regex 模式的大寫開頭函式名稱在沒有new
運算子的情況下呼叫。"properties": true
(預設)啟用物件屬性的檢查"properties": false
停用物件屬性的檢查
newIsCap
使用預設 { "newIsCap": true }
選項時,此規則的 不正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCap": true }]*/
const friend = new ();
使用預設 { "newIsCap": true }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCap": true }]*/
const friend = new Person();
使用 { "newIsCap": false }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCap": false }]*/
const friend = new person();
capIsNew
使用預設 { "capIsNew": true }
選項時,此規則的 不正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNew": true }]*/
const colleague = ();
使用預設 { "capIsNew": true }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNew": true }]*/
const colleague = new Person();
使用 { "capIsNew": false }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNew": false }]*/
const colleague = Person();
newIsCapExceptions
使用 { "newIsCapExceptions": ["events"] }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
const events = require('events');
const emitter = new events();
newIsCapExceptionPattern
使用 { "newIsCapExceptionPattern": "^person\\.." }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/
const friend = new person.acquaintance();
const bestFriend = new person.friend();
使用 { "newIsCapExceptionPattern": "\\.bar$" }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/
const friend = new person.bar();
capIsNewExceptions
使用 { "capIsNewExceptions": ["Person"] }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
function foo(arg) {
return Person(arg);
}
capIsNewExceptionPattern
使用 { "capIsNewExceptionPattern": "^person\\.." }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/
const friend = person.Acquaintance();
const bestFriend = person.Friend();
使用 { "capIsNewExceptionPattern": "\\.Bar$" }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/
foo.Bar();
使用 { "capIsNewExceptionPattern": "^Foo" }
選項時,此規則的其他 正確 程式碼範例
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/
const x = Foo(42);
const y = Foobar(42);
const z = Foo.Bar(42);
properties
使用預設 { "properties": true }
選項時,此規則的 不正確 程式碼範例
/*eslint new-cap: ["error", { "properties": true }]*/
const friend = new person.();
使用預設 { "properties": true }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "properties": true }]*/
const friend = new person.Acquaintance();
使用 { "properties": false }
選項時,此規則的 正確 程式碼範例
/*eslint new-cap: ["error", { "properties": false }]*/
const friend = new person.acquaintance();
何時不該使用
如果您有慣例不要求建構子使用大寫字母,或者不要求大寫函式僅用作建構子,請關閉此規則。
版本
此規則在 ESLint v0.0.3-0 中引入。