Skip to content
← Back to rules

eslint/no-extra-bind 可疑

An auto-fix is available for this rule.

它做了什么

禁止不必要的 .bind() 调用

为什么这是个问题?

此规则旨在避免不必要地使用 bind(), 因此当立即执行的函数表达式(IIFE)使用了 bind() 但没有合适的 this 值时,将会发出警告。 此规则不会标记包含函数参数绑定的 bind() 使用情况。

示例

此规则的错误代码示例:

js
const x = function () {
  foo();
}.bind(bar);

const z = (() => {
  this.foo();
}).bind(this);

此规则的正确代码示例:

js
const x = function () {
  this.foo();
}.bind(bar);
const y = function (a) {
  return a + 1;
}.bind(foo, bar);

如何使用

通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "no-extra-bind": "error"
  }
}
bash
oxlint --deny no-extra-bind

参考资料