unicorn/no-array-method-this-argument 风格
作用
禁止在数组迭代方法(如 map、filter、some、every 等)中使用 thisArg 参数。
为什么这是不好的?
thisArg 参数会使代码更难理解与推理。相反,建议使用箭头函数,或以更清晰的方式显式绑定 this。箭头函数会从词法作用域继承 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;
});如何使用
通过配置文件或 CLI 启用 此规则,可使用以下方式:
json
{
"rules": {
"unicorn/no-array-method-this-argument": "error"
}
}bash
oxlint --deny unicorn/no-array-method-this-argument