Skip to content
← Back to rules

unicorn/no-array-method-this-argument Style

🚧 An auto-fix is planned for this rule, but not implemented at this time.

作用

禁止在数组迭代方法中使用 thisArg 参数,例如 mapfiltersomeevery 等。

为什么这不好?

thisArg 参数会让代码更难理解和推理。相反, 应优先使用箭头函数,或者以更清晰的方式显式绑定。箭头函数会从词法作用域继承 this,这更直观,也更不容易出错。

示例

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

js
array.map(function (x) {
  return x + this.y;
}, this);
array.filter(function (x) {
  return x !== this.value;
}, this);

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

js
array.map((x) => x + this.y);
array.filter((x) => x !== this.value);
const self = this;
array.map(function (x) {
  return x + self.y;
});

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-array-method-this-argument": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-array-method-this-argument": "error",
  },
});
bash
oxlint --deny unicorn/no-array-method-this-argument

版本

此规则于 v0.16.12 中添加。

参考资料