Home > Mobile >  ESLint - rules that avoid unnecessary `var that = this` pattern
ESLint - rules that avoid unnecessary `var that = this` pattern

Time:09-26

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.

CodePudding user response:

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

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

CodePudding user response:

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.

  • Related