unicorn/no-this-assignment 严格
它做了什么
禁止将 this 赋值给变量。
为什么这是不好的?
将 this 赋值给变量是不必要的,且容易引起混淆。
示例
此规则的 错误 代码示例:
javascript
const foo = this;
class Bar {
method() {
foo.baz();
}
}
new Bar().method();此规则的 正确 代码示例:
javascript
class Bar {
constructor(fooInstance) {
this.fooInstance = fooInstance;
}
method() {
this.fooInstance.baz();
}
}
new Bar(this).method();如何使用
要通过配置文件或在 CLI 中 启用 此规则,可以使用:
json
{
"rules": {
"unicorn/no-this-assignment": "error"
}
}bash
oxlint --deny unicorn/no-this-assignment