Skip to content

Latest commit

Β 

History

History
60 lines (44 loc) Β· 1.42 KB

no-this-assignment.md

File metadata and controls

60 lines (44 loc) Β· 1.42 KB

no-this-assignment

πŸ“ 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();

Related rules

  • @typescript-eslint/no-this-alias - Equivalent rule from typescript-eslint. If you already use that one, you don't need this rule. It has allowDestructuring (enabled by default) and allowedNames options.