func-names
要求或禁止命名的 function 表达式
一种变得越来越常见的做法是给函数表达式命名以帮助调试。例如:
🌐 A pattern that’s becoming more common is to give function expressions names to aid in debugging. For example:
Foo.prototype.bar = function bar() {};
在上面的示例中添加第二个 bar 是可选的。如果省略函数名称,那么当函数抛出异常时,你很可能会在堆栈跟踪中看到类似 anonymous function 的内容。如果为函数表达式提供可选的名称,那么你将在堆栈跟踪中看到函数表达式的名称。
🌐 Adding the second bar in the above example is optional. If you leave off the function name then when the function throws an exception you are likely to get something similar to anonymous function in the stack trace. If you provide the optional name for a function expression then you will get the name of the function expression in the stack trace.
规则详情
🌐 Rule Details
此规则可以强制或禁止使用命名函数表达式。
🌐 This rule can enforce or disallow the use of named function expressions.
选项
🌐 Options
此规则有一个字符串选项:
🌐 This rule has a string option:
"always"(默认)要求函数表达式必须有名称。"as-needed"要求函数表达式必须有一个名称,如果根据 ECMAScript 规范名称没有自动分配的话。"never"不允许命名函数表达式,除非在递归函数中,需要使用名字。
此规则有一个对象选项:
🌐 This rule has an object option:
"generators": "always" | "as-needed" | "never""always"需要命名的生成器。"as-needed"要求在未根据 ECMAScript 规范自动分配名称时命名生成器。"never"尽可能禁止命名生成器。
当未提供 generators 的值时,生成器函数的行为将回退到基本选项。
🌐 When a value for generators is not provided the behavior for generator functions falls back to the base option.
请注意,"always" 和 "as-needed" 要求 export default 声明中的函数表达式和函数声明必须有名称。
🌐 Please note that "always" and "as-needed" require function expressions and function declarations in export default declarations to have a name.
always
使用默认 "always" 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function() {};
const cat = {
meow: function() {}
}
(function() {
// ...
}())
export default function() {}
使用默认 "always" 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default "always" option:
/*eslint func-names: ["error", "always"]*/
Foo.prototype.bar = function bar() {};
const cat = {
meow() {}
}
(function bar() {
// ...
}())
export default function foo() {}
as-needed
ECMAScript 6 在所有函数上引入了一个 name 属性。name 的值是通过评估函数周围的代码来确定是否可以推断出名称。例如,分配给变量的函数会自动拥有一个 name 属性,其值等于变量的名称。name 的值随后在堆栈跟踪中使用,以便于调试。
🌐 ECMAScript 6 introduced a name property on all functions. The value of name is determined by evaluating the code around the function to see if a name can be inferred. For example, a function assigned to a variable will automatically have a name property equal to the name of the variable. The value of name is then used in stack traces for easier debugging.
使用 "as-needed" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
export default function() {}
使用 "as-needed" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "as-needed" option:
/*eslint func-names: ["error", "as-needed"]*/
const bar = function() {};
const cat = {
meow: function() {}
}
class C {
#bar = function() {};
baz = function() {};
}
quux ??= function() {};
(function bar() {
// ...
}())
export default function foo() {}
never
使用 "never" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never" option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function bar() {};
(function bar() {
// ...
}())
使用 "never" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "never" option:
/*eslint func-names: ["error", "never"]*/
Foo.prototype.bar = function() {};
(function() {
// ...
}())
generators
使用 "always", { "generators": "as-needed" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always", { "generators": "as-needed" } options:
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
(function*() {
// ...
}())
使用 "always", { "generators": "as-needed" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always", { "generators": "as-needed" } options:
/*eslint func-names: ["error", "always", { "generators": "as-needed" }]*/
const foo = function*() {};
此规则使用 "always", { "generators": "never" } 选项的 错误 代码示例:
🌐 Examples of incorrect code for this rule with the "always", { "generators": "never" } options:
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
const foo = bar(function *baz() {});
使用 "always", { "generators": "never" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always", { "generators": "never" } options:
/*eslint func-names: ["error", "always", { "generators": "never" }]*/
const foo = bar(function *() {});
此规则使用 "as-needed", { "generators": "never" } 选项的 错误 代码示例:
🌐 Examples of incorrect code for this rule with the "as-needed", { "generators": "never" } options:
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
const foo = bar(function *baz() {});
使用 "as-needed", { "generators": "never" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "as-needed", { "generators": "never" } options:
/*eslint func-names: ["error", "as-needed", { "generators": "never" }]*/
const foo = bar(function *() {});
使用 "never", { "generators": "always" } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "never", { "generators": "always" } options:
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
const foo = bar(function *() {});
使用 "never", { "generators": "always" } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "never", { "generators": "always" } options:
/*eslint func-names: ["error", "never", { "generators": "always" }]*/
const foo = bar(function *baz() {});
兼容性
🌐 Compatibility
- JSCS: requireAnonymousFunctions
- JSCS: disallowAnonymousFunctions
版本
此规则是在 ESLint v0.4.0 中引入。