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();