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 using the config file or in the CLI, you can use:

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

References

Released under the MIT License.