Skip to content

Files

Latest commit

 

History

History
35 lines (27 loc) · 591 Bytes

no-this-assignment.md

File metadata and controls

35 lines (27 loc) · 591 Bytes

Pattern: Assignment of this to variable

Issue: -

Description

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.

Examples

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