Skip to content
← Back to rules

unicorn/no-this-assignment Pedantic

它的作用

禁止将 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();

如何使用

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

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

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

版本

此规则于 v0.0.18 中添加。

参考资料