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"(默认)要求函数表达式有名称

    ¥"always" (default) requires function expressions to have a name

  • 如果未按照 ECMAScript 规范自动分配名称,则 "as-needed" 要求函数表达式具有名称。

    ¥"as-needed" requires function expressions to have a name, if the name isn’t assigned automatically per the ECMAScript specification.

  • "never" 不允许命名函数表达式,除非在需要名称的递归函数中

    ¥"never" disallows named function expressions, except in recursive functions, where a name is needed

此规则有一个对象选项:

¥This rule has an object option:

  • "generators": "always" | "as-needed" | "never"

    • "always" 需要命名生成器

      ¥"always" require named generators

    • 如果没有按照 ECMAScript 规范自动分配名称,则 "as-needed" 需要命名生成器。

      ¥"as-needed" require named generators if the name isn’t assigned automatically per the ECMAScript specification.

    • "never" 尽可能禁止命名生成器。

      ¥"never" disallow named generators where possible.

当未提供 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"]*/

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

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

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

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

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

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

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

var foo = bar(function *baz() {});

兼容性

¥Compatibility

版本

此规则是在 ESLint v0.4.0 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号