Skip to content

unicorn/no-this-assignment Pedantic

What it does

Disallow assigning this to a variable.

Why is this bad?

Assigning this to a variable is unnecessary and confusing.

Examples

Examples of incorrect code for this rule:

javascript
const foo = this;
class Bar {
  method() {
    foo.baz();
  }
}

new Bar().method();

Examples of correct code for this rule:

javascript
class Bar {
  constructor(fooInstance) {
    this.fooInstance = fooInstance;
  }
  method() {
    this.fooInstance.baz();
  }
}

new Bar(this).method();

How to use

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

bash
oxlint --deny unicorn/no-this-assignment
json
{
  "rules": {
    "unicorn/no-this-assignment": "error"
  }
}

References

Released under the MIT License.