版本

new-cap

要求构造函数名称以大写字母开头

JavaScript 中的 new 运算符创建一个特定类型的对象的新实例。对象类型由构造函数表示。由于构造函数只是常规函数,因此唯一的定义特征是 new 被用作调用的一部分。原生 JavaScript 函数以大写字母开头,以区分那些用作构造函数的函数和不用作构造函数的函数。许多样式指南建议遵循此模式,以便更容易地确定哪些函数将用作构造函数。

const friend = new Person();

规则详情

此规则要求构造函数名称以大写字母开头。某些内置标识符可免除此规则。这些标识符是

  • Array
  • Boolean
  • Date
  • Error
  • Function
  • Number
  • Object
  • RegExp
  • String
  • Symbol
  • BigInt

此规则的正确代码示例

在 Playground 中打开
/*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 } 选项时,此规则的不正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCap": true }]*/

const friend = new person();

使用默认 { "newIsCap": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCap": true }]*/

const friend = new Person();

使用 { "newIsCap": false } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCap": false }]*/

const friend = new person();

capIsNew

使用默认 { "capIsNew": true } 选项时,此规则的不正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNew": true }]*/

const colleague = Person();

使用默认 { "capIsNew": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNew": true }]*/

const colleague = new Person();

使用 { "capIsNew": false } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNew": false }]*/

const colleague = Person();

newIsCapExceptions

使用 { "newIsCapExceptions": ["events"] } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/

const events = require('events');

const emitter = new events();

newIsCapExceptionPattern

使用 { "newIsCapExceptionPattern": "^person\\.." } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/

const friend = new person.acquaintance();

const bestFriend = new person.friend();

使用 { "newIsCapExceptionPattern": "\\.bar$" } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/

const friend = new person.bar();

capIsNewExceptions

使用 { "capIsNewExceptions": ["Person"] } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/

function foo(arg) {
    return Person(arg);
}

capIsNewExceptionPattern

使用 { "capIsNewExceptionPattern": "^person\\.." } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/

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

使用 { "capIsNewExceptionPattern": "\\.Bar$" } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/

foo.Bar();

使用 { "capIsNewExceptionPattern": "^Foo" } 选项时,此规则的附加正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/

const x = Foo(42);

const y = Foobar(42);

const z = Foo.Bar(42);

properties

使用默认 { "properties": true } 选项时,此规则的不正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "properties": true }]*/

const friend = new person.acquaintance();

使用默认 { "properties": true } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "properties": true }]*/

const friend = new person.Acquaintance();

使用 { "properties": false } 选项时,此规则的正确代码示例

在 Playground 中打开
/*eslint new-cap: ["error", { "properties": false }]*/

const friend = new person.acquaintance();

何时不使用

如果您有约定不需要构造函数使用大写字母,或者不需要大写函数仅用作构造函数,请关闭此规则。

版本

此规则在 ESLint v0.0.3-0 中引入。

资源

更改语言