---
url: /docs/guide/usage/linter/rules/eslint/prefer-arrow-callback.md
---

### 作用

要求回调使用箭头函数。

### 为什么这不好？

箭头函数通常更适合用于回调，因为它们：

* 从外围作用域继承 `this`，避免了常见的错误来源；
* 更简短，也更易于阅读；
* 不能用作构造函数，这一点对回调来说是理想的。

### 示例

以下是此规则的**错误**代码示例：

```js
foo(function (a) {
  return a;
});
foo(
  function () {
    return this.a;
  }.bind(this),
);
```

以下是此规则的**正确**代码示例：

```js
foo((a) => a);
foo(function* () {
  yield;
});
foo(function () {
  this;
});
foo(function bar() {
  bar();
});
```

## 配置

### allowNamedFunctions

type: `boolean`

default: `false`

如果将此选项设置为 `true`，则允许命名函数表达式。

### allowUnboundThis

type: `boolean`

default: `true`

如果将此选项设置为 `false`，即使函数表达式未绑定到 `this` 值，引用了 `this` 的函数表达式也会被报告。

## 如何使用

## 版本

此规则于 v1.65.0 中添加。

## 参考资料
