版本

new-cap

要求建構子名稱以大寫字母開頭

JavaScript 中的 new 運算子會建立特定類型的物件的新實例。該類型的物件由建構子函式表示。由於建構子函式只是普通的函式,因此唯一的定義特性是 new 用作呼叫的一部分。原生 JavaScript 函式以大寫字母開頭,以區分那些要用作建構子的函式與那些不是的函式。許多風格指南建議遵循此模式,以便更容易確定哪些函式要用作建構子。

var 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" 允許任何符合指定正規表示式模式的以小寫字母開頭的函式名稱以 new 運算子呼叫。
  • "capIsNewExceptions" 允許指定的以大寫字母開頭的函式名稱不使用 new 運算子呼叫。
  • "capIsNewExceptionPattern" 允許任何符合指定正規表示式模式的以大寫字母開頭的函式名稱不使用 new 運算子呼叫。
  • "properties": true (預設) 啟用對物件屬性的檢查
  • "properties": false 停用對物件屬性的檢查

newIsCap

使用預設 { "newIsCap": true } 選項時,此規則的錯誤程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCap": true }]*/

var friend = new person();

使用預設 { "newIsCap": true } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCap": true }]*/

var friend = new Person();

使用 { "newIsCap": false } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCap": false }]*/

var friend = new person();

capIsNew

使用預設 { "capIsNew": true } 選項時,此規則的錯誤程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "capIsNew": true }]*/

var colleague = Person();

使用預設 { "capIsNew": true } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "capIsNew": true }]*/

var colleague = new Person();

使用 { "capIsNew": false } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "capIsNew": false }]*/

var colleague = Person();

newIsCapExceptions

使用 { "newIsCapExceptions": ["events"] } 選項時,此規則的額外正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/

var events = require('events');

var emitter = new events();

newIsCapExceptionPattern

使用 { "newIsCapExceptionPattern": "^person\\.." } 選項時,此規則的額外正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/

var friend = new person.acquaintance();

var bestFriend = new person.friend();

使用 { "newIsCapExceptionPattern": "\\.bar$" } 選項時,此規則的額外正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/

var 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\\.." }]*/

var friend = person.Acquaintance();
var bestFriend = person.Friend();

使用 { "capIsNewExceptionPattern": "\\.Bar$" } 選項時,此規則的額外正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/

foo.Bar();

使用 { "capIsNewExceptionPattern": "^Foo" } 選項時,此規則的額外正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/

var x = Foo(42);

var y = Foobar(42);

var z = Foo.Bar(42);

properties

使用預設 { "properties": true } 選項時,此規則的錯誤程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "properties": true }]*/

var friend = new person.acquaintance();

使用預設 { "properties": true } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "properties": true }]*/

var friend = new person.Acquaintance();

使用 { "properties": false } 選項時,此規則的正確程式碼範例

在線上執行環境中開啟
/*eslint new-cap: ["error", { "properties": false }]*/

var friend = new person.acquaintance();

何時不該使用

如果您有不要求建構子使用大寫字母,或不要求僅以大寫字母開頭的函式用作建構子的慣例,請關閉此規則。

版本

此規則在 ESLint v0.0.3-0 中引入。

資源

變更語言