no-this-assignment
March 27, 2026 ยท View on GitHub
๐ Disallow assigning this to a variable.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
this should be used directly. If you want a reference to this from a higher scope, consider using arrow function expression or Function#bind().
Examples
// โ
const foo = this;
setTimeout(function () {
foo.bar();
}, 1000);
// โ
setTimeout(() => {
this.bar();
}, 1000);
// โ
setTimeout(function () {
this.bar();
}.bind(this), 1000);
// โ
const foo = this;
class Bar {
method() {
foo.baz();
}
}
new Bar().method();
// โ
class Bar {
constructor(fooInstance) {
this.fooInstance = fooInstance;
}
method() {
this.fooInstance.baz();
}
}
new Bar(this).method();