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);
}
选项
i/QWu02im19/lo5J+rk5/ZR4XOv+gnAKNAo2YIGQQRhKcl4h+hJrX6M9JueM06zl
- 1wpWTm1a3fEQ9O++ta4gI7weffSAKmbEapWesC2Xghm86yTfFvR12sNuFXMoDQfVIPobKzZ4qfDF9rfuf/YezpcUA0wCfI/mimAsCALrIQXk7mUd9z1qBlH3l1sb6BSLfn/P/4FtxXVZI/bsb410lOrgQUrUiTYZxBbqCuaBWJi1BLccR50rh5B/W3VCCfy/nXFVBCuomUJFCE7/xePWj/MYFQGWR/XYc9AtixbVteLJ4+jxYv0ldHU6WDiXBifB5YBW5Uw4lL/wqjPrzYgroJK/vBRhTYge6wt4AgOArAv7KHRoBaPYRPOYrC7ajOHxeB0NRrCIJn378V1jwMSvJdbEL+/nT7qOZR/7CiShOE5cKAnDUFPUGjkpRqfA07sfkeErROs/RmYkOAKoYEAJuS/2G9Z04haSmHIBMhUOGDHqyY6rwBIGE8fsimVQJ9QFEo6y8v+lH0R0gAPdAHSgHm01U2pXhyvbVeZry+tr0R2ZPiFPh2QSNkyxVLMSMMb8TJe7HpqEIY+0F09oe3fqIm2X3yqvYlonHnYaYVfllKmT81mAVUGZPq5tvuNssCYAy/BITX0Q1H0zU63tWkjI/z9WRhG3eX6xlqAbQJ3Qcp3K6m3wfUAt69uxJsmEkkuugaHJLR871TDRjlQCQ0zmlPY5oMp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cqJqdYDk3bvBWmlR950EyEYUXUadQOS5VS1Ss6T9s+vPnX4Fto3LwgZSgoFwIIxW2GIXCWpjXUf1Lf1ao4RM70J/3A46BihIGaHv8vL4fQXgrIn
/*eslint new-cap: ["error", { "newIsCap": true }]*/
var friend = new person();
0UzW5qZvZRhpd4yfdcqJqdYDk3bvBWmlR950EyEYUXUadQOS5VS1Ss6T9s+vPnX4Fto3LwgZSgoFwIIxW2GIXErrWXMKwpwDxq30Slpm2JDhJ8RNdmoLxQJQueq6p3Jj
/*eslint new-cap: ["error", { "newIsCap": true }]*/
var friend = new Person();
vLn68i7rFOv8nwd/Nio84236+w8Iwahu86mVpKe1fZ931Qx2u5cPm6Z+eNLr8tCuDpJH0Iep4gQjvDpO6MmIYMTwJ/McxMJxjclGS0ni+olcLb7PsXIEhdcbUywykyJn
/*eslint new-cap: ["error", { "newIsCap": false }]*/
var friend = new person();
capIsNew
0UzW5qZvZRhpd4yfdcqJqWOYJSJXCEg6xR+jiBMP5xQKsLO2/pKBE6d5dS+uIK7lvnsU6wr23kwLuKJ65cZYQDuPyiu8tRyyWzNL6pryjgLl+IxqVztn4KGrfHes3kjO
/*eslint new-cap: ["error", { "capIsNew": true }]*/
var colleague = Person();
0UzW5qZvZRhpd4yfdcqJqWOYJSJXCEg6xR+jiBMP5xQKsLO2/pKBE6d5dS+uIK7lvnsU6wr23kwLuKJ65cZYQFW3pR4FPTs5vMe2ZIeBxuh099q8pxr6I3e2ObGwsHdK
/*eslint new-cap: ["error", { "capIsNew": true }]*/
var colleague = new Person();
vLn68i7rFOv8nwd/Nio845lZro1Appr/PDmZzCv69BV7sgynzT9URekFgZXv2XZUYAHHTRRVnIMFKXy7bTe/5TM7iGNMmEvR4DRXPwxUJglKOOEg1SztWtzAn8HS+pP2
/*eslint new-cap: ["error", { "capIsNew": false }]*/
var colleague = Person();
newIsCapExceptions
vLn68i7rFOv8nwd/Nio84236+w8Iwahu86mVpKe1fZ/UXXorr/zZB8M6HyHvrTg00BLvuIluXm5NpK/W35vAUUlfrnXKfzD+i1nc724HegohaTAt5WRIQ2jPOHvPsml0qjImFEF10Tu+5Ft7hhZszw==
/*eslint new-cap: ["error", { "newIsCapExceptions": ["events"] }]*/
var events = require('events');
var emitter = new events();
newIsCapExceptionPattern
vLn68i7rFOv8nwd/Nio84236+w8Iwahu86mVpKe1fZ9EMR/zEER22uYzcmPHeFumUlmtW9eOzNw4Su2PG1KGXhsnjwKJ5Xzz3j87pMaNLzoxGfgjT0J4K/ElP16a7RX3uq77UAVbGIXg4aO3tmpHJQ==
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "^person\\.." }]*/
var friend = new person.acquaintance();
var bestFriend = new person.friend();
vLn68i7rFOv8nwd/Nio84236+w8Iwahu86mVpKe1fZ9EMR/zEER22uYzcmPHeFum9K123tLz99YKis5aOSFze7SyF9a/DVfBGW3e/a4n6wr9bTRFEqkHr6UayvDFsN5VfbhLHHgTsrBQXNAEM8Dy4Q==
/*eslint new-cap: ["error", { "newIsCapExceptionPattern": "\\.bar$" }]*/
var friend = new person.bar();
capIsNewExceptions
vLn68i7rFOv8nwd/Nio845lZro1Appr/PDmZzCv69BUremjaS3zyABf6wqGbeJV7pZ5hRWbkB0S5+EbhG4XkMpo3azTgm9COrvpk9EZxjGrctSnOl43asdVAjajXorj8Qm4TJ1wH855Bb8aIFjldfQ==
/*eslint new-cap: ["error", { "capIsNewExceptions": ["Person"] }]*/
function foo(arg) {
return Person(arg);
}
capIsNewExceptionPattern
vLn68i7rFOv8nwd/Nio845lZro1Appr/PDmZzCv69BWoMqWJ7YZfYZS81wzGvJOqkxDm2s/k2jXy9cJq5z6nkW9U3L7+ED98h9T7NocbjRqFOmwQkV2vrqRwFC/LNLryXw9pA0by0AGd5fvqA63rKg==
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^person\\.." }]*/
var friend = person.Acquaintance();
var bestFriend = person.Friend();
vLn68i7rFOv8nwd/Nio845lZro1Appr/PDmZzCv69BWoMqWJ7YZfYZS81wzGvJOqpROqA4mUJJuV2/BxVSP4wwEaDC69zHn7jGwwftb3DicMpfA3muVWIh686tdHyu/nF9CB5AqiCwzDLplrU7YxqA==
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "\\.Bar$" }]*/
foo.Bar();
vLn68i7rFOv8nwd/Nio845lZro1Appr/PDmZzCv69BWoMqWJ7YZfYZS81wzGvJOqeZ0kw4/8YRNCaXKYV97rH0BmRv9NQ7YaLSyWZzCfR6POd0E8858UEoJyoP3P8yP53ERnlCQmj8wB2PbJd3pKjw==
/*eslint new-cap: ["error", { "capIsNewExceptionPattern": "^Foo" }]*/
var x = Foo(42);
var y = Foobar(42);
var z = Foo.Bar(42);
properties
0UzW5qZvZRhpd4yfdcqJqTrsnB9wyOTsjZhZOwL/7Qkyi6smsWnBrC5UtUZN80S63Cp1em2XbSHEKsQ6E+LAal+crG2VlOdmC5LW9Ax19XsLa/2Npk5e6VB3MGZxXYGy
/*eslint new-cap: ["error", { "properties": true }]*/
var friend = new person.acquaintance();
0UzW5qZvZRhpd4yfdcqJqTrsnB9wyOTsjZhZOwL/7Qkyi6smsWnBrC5UtUZN80S63Cp1em2XbSHEKsQ6E+LAatD3+PRwwir2MCqXo8WgNMHPir3EZkyCdmsRKR4CBUzJ
/*eslint new-cap: ["error", { "properties": true }]*/
var friend = new person.Acquaintance();
vLn68i7rFOv8nwd/Nio844hJ7z5xN6CmmwzEj7G2y3d8Wwf04ocKGA3Qyd9McDlzoeVZ3YAVZDNn2HPXWEUewXJoP8wC/HizpK6H6uK4h98gVd8gCx21uD3088X0qt/B
/*eslint new-cap: ["error", { "properties": false }]*/
var friend = new person.acquaintance();
何时不使用
RivggyMme0SU0VnaGPF/G9/SY/88eBQ8XzP022C4Ga2tqj28OboD0jXprHRv9m+WvG7EHM7rOHlsbWDrxy8xwiRIjqsXXTCPFQQJN824wDSxT0YubtHyb0rPJLyHU/1A8+GOebD+0JoPpND1LJA3xbaF9CmwocHzhu6Q4azGf3HVDBhs/L6jRKwFnyniA2Zx