Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.14 KB

File metadata and controls

56 lines (42 loc) · 1.14 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();