Require using arrow functions for callbacks (prefer-arrow-callback)

要求使用箭头函数作为回调 (prefer-arrow-callback)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

Arrow functions can be an attractive alternative to function expressions for callbacks or function arguments.


For example, arrow functions are automatically bound to their surrounding scope/context. This provides an alternative to the pre-ES6 standard of explicitly binding function expressions to achieve similar behavior.

例如,箭头函数自动绑定到其周围作用域/上下文。这为 ES6 之前的标准的显式绑定函数表达式提供了一个替代,来实现类似的行为。

Additionally, arrow functions are:


Rule Details

This rule locates function expressions used as callbacks or function arguments. An error will be produced for any that could be replaced by an arrow function without changing the result.


The following examples will be flagged:


/* eslint prefer-arrow-callback: "error" */

foo(function(a) { return a; }); // ERROR
// prefer: foo(a => a)

foo(function() { return this.a; }.bind(this)); // ERROR
// prefer: foo(() => this.a)

Instances where an arrow function would not produce identical results will be ignored.


The following examples will not be flagged:


/* eslint prefer-arrow-callback: "error" */
/* eslint-env es6 */

// arrow function callback
foo(a => a); // OK

// generator as callback
foo(function*() { yield; }); // OK

// function expression not used as callback or function argument
var foo = function foo(a) { return a; }; // OK

// unbound function expression callback
foo(function() { return this.a; }); // OK

// recursive named function callback
foo(function bar(n) { return n && n + bar(n - 1); }); // OK


Access further control over this rule’s behavior via an options object.


Default: { allowNamedFunctions: false, allowUnboundThis: true }

默认:{ allowNamedFunctions: false, allowUnboundThis: true }


By default { "allowNamedFunctions": false }, this boolean option prohibits using named functions as callbacks or function arguments.

默认 { "allowNamedFunctions": false },该 boolean 选项禁止使用命名的函数作为回调函数或函数参数。

Changing this value to true will reverse this option’s behavior by allowing use of named functions without restriction.

将此值改为 true,将反转此选项的行为,允许使用命名的函数作为回调函数或函数参数。

{ "allowNamedFunctions": true } will not flag the following example:

{ "allowNamedFunctions": true } 将不会标记以下示例:

/* eslint prefer-arrow-callback: [ "error", { "allowNamedFunctions": true } ] */

foo(function bar() {});


By default { "allowUnboundThis": true }, this boolean option allows function expressions containing this to be used as callbacks, as long as the function in question has not been explicitly bound.

默认 { "allowUnboundThis": true },该 boolean 选项允许包含 this 的函数表达式被用作回调函数,只要问题函数没有被显式绑定。

When set to false this option prohibits the use of function expressions as callbacks or function arguments entirely, without exception.

当设置为 false 时,该选项完全禁止函数表达式作为回调函数或函数参数,无一例外。

{ "allowUnboundThis": false } will flag the following examples:

{ "allowUnboundThis": false } 将标记以下示例:

/* eslint prefer-arrow-callback: [ "error", { "allowUnboundThis": false } ] */
/* eslint-env es6 */

foo(function() { this.a; });

foo(function() { (() => this); }); { return this.doSomething(itm); }, someObject);

When Not To Use It

Further Reading


This rule was introduced in ESLint 1.2.0.

该规则在 ESLint 1.2.0 中被引入。