Pattern: Assignment of this
to variable
Issue: -
Storing this
in a variable creates unnecessary aliases that can make code more confusing. Instead, use proper binding techniques or pass this
as a parameter when needed.
Example of incorrect code:
const foo = this;
class Bar {
method() {
foo.baz();
}
}
new Bar().method();
Example of correct code:
class Bar {
constructor(fooInstance) {
this.fooInstance = fooInstance;
}
method() {
this.fooInstance.baz();
}
}
new Bar(this).method();