Index

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.

const 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": false 允许用小写或大写开头的函数来调用 new 运算符。
  • "capIsNew": true(默认)要求所有以大写字母开头的函数都必须使用 new 操作符调用。
  • "capIsNew": false 允许以大写字母开头的函数在不使用 new 运算符的情况下被调用。
  • "newIsCapExceptions" 允许使用 new 操作符调用指定的小写开头的函数名。
  • "newIsCapExceptionPattern" 允许任何以小写字母开头且匹配指定正则表达式模式的函数名称使用 new 运算符调用。
  • "capIsNewExceptions" 允许以大写字母开头的指定函数名在不使用 new 运算符的情况下被调用。
  • "capIsNewExceptionPattern" 允许任何以大写字母开头且匹配指定正则表达式模式的函数名称在不使用 new 操作符的情况下被调用。
  • "properties": true(默认)启用对对象属性的检查。
  • "properties": false 禁用对对象属性的检查。

newIsCap

使用默认 { "newIsCap": true } 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default { "newIsCap": true } option:

在线运行
/*eslint new-cap: ["error", { "newIsCap": true }]*/

const friend = new person();

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

🌐 Examples of correct code for this rule with the default { "newIsCap": true } option:

在线运行
/*eslint new-cap: ["error", { "newIsCap": true }]*/

const friend = new Person();

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

🌐 Examples of correct code for this rule with the { "newIsCap": false } option:

在线运行
/*eslint new-cap: ["error", { "newIsCap": false }]*/

const 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 }]*/

const colleague = Person();

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

🌐 Examples of correct code for this rule with the default { "capIsNew": true } option:

在线运行
/*eslint new-cap: ["error", { "capIsNew": true }]*/

const colleague = new Person();

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

🌐 Examples of correct code for this rule with the { "capIsNew": false } option:

在线运行
/*eslint new-cap: ["error", { "capIsNew": false }]*/

const colleague = Person();

newIsCapExceptions

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

🌐 Examples of additional correct code for this rule with the { "newIsCapExceptions": ["events"] } option:

在线运行
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/

const events = require('events');

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

const friend = new person.acquaintance();

const 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$" }]*/

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

const friend = person.Acquaintance();
const 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" }]*/

const x = Foo(42);

const y = Foobar(42);

const 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 }]*/

const 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 }]*/

const friend = new person.Acquaintance();

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

🌐 Examples of correct code for this rule with the { "properties": false } option:

在线运行
/*eslint new-cap: ["error", { "properties": false }]*/

const 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 中引入。

资源