1 # Require Consistent This (consistent-this)
3 It is often necessary to capture the current execution context in order to make it available subsequently. A prominent example of this are jQuery callbacks:
7 jQuery('li').click(function (event) {
8 // here, "this" is the HTMLElement where the click event occurred
13 There are many commonly used aliases for `this` such as `that`, `self` or `me`. It is desirable to ensure that whichever alias the team agrees upon is used consistently throughout the application.
17 This rule enforces two things about variables with the designated alias names for `this`:
19 * If a variable with a designated name is declared, it *must* be either initialized (in the declaration) or assigned (in the same scope as the declaration) the value `this`.
20 * If a variable is initialized or assigned the value `this`, the name of the variable *must* be a designated alias.
24 This rule has one or more string options:
26 * designated alias names for `this` (default `"that"`)
28 Examples of **incorrect** code for this rule with the default `"that"` option:
31 /*eslint consistent-this: ["error", "that"]*/
42 Examples of **correct** code for this rule with the default `"that"` option:
45 /*eslint consistent-this: ["error", "that"]*/
58 Examples of **incorrect** code for this rule with the default `"that"` option, if the variable is not initialized:
61 /*eslint consistent-this: ["error", "that"]*/
69 Examples of **correct** code for this rule with the default `"that"` option, if the variable is not initialized:
72 /*eslint consistent-this: ["error", "that"]*/
84 If you need to capture nested context, `consistent-this` is going to be problematic. Code of that nature is usually difficult to read and maintain and you should consider refactoring it.