Stephen.W
Stephen.W

Reputation: 2127

ESLint - rules that avoid unnecessary `var that = this` pattern

Before the arrow function is introduced, it was quite common to assign this to a variable that is used inside a callback. for example, with JQuery, people may write:

/* Omit the definition of `App` */

App.prototype.init = function () {
    var that = this;

    $(`#btn`).on("click", function () {
        that.popUpDialog("Hello!");
    });
}

Now since we have arrow function, most of the time we just don't need to write such verbose codes - we can simply use this inside the function body, so I think if ESLint has a rule that bans all unnecessary this assignments, in order to keep the codes clean and readable.

Upvotes: 0

Views: 83

Answers (2)

Fraser
Fraser

Reputation: 17039

The only thing I can think of is the consistent-this rule. See https://eslint.org/docs/latest/rules/consistent-this

However that is used to

"enforce consistent naming when capturing the current execution context"

Not to explicitly prevent its use.

Upvotes: 1

Nicky McCurdy
Nicky McCurdy

Reputation: 19524

You're looking for consistent-this with an arbitrary unused placeholder name:

"consistent-this": ["error", "placeholder for invalid variable name"]

Upvotes: 0

Related Questions