【问题标题】:ESLint - rules that avoid unnecessary `var that = this` patternESLint - 避免不必要的 `var that = this` 模式的规则
【发布时间】:2022-09-26 16:54:06
【问题描述】:

在引入箭头函数之前,将this 分配给回调中使用的变量是很常见的。例如,使用 JQuery,人们可能会写:

/* Omit the definition of `App` */

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

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

现在既然我们有了箭头函数,大多数时候我们就不需要写这么冗长的代码了——我们可以简单地在函数体内使用this,所以我想如果ESLint有一个规则可以禁止所有不必要的@987654325 @ assignments,以保持代码简洁易读。

  • 您能否提供 ESLint 警告的确切措辞。
  • 也许this existing rule 会有所帮助。
  • 在同一个函数中你永远不需要thisthat 吗?像这样的规则似乎有点限制并且不是非常整洁(尽管如果它适合您的代码库则完全有效)

标签: javascript eslint


【解决方案1】:

您正在寻找具有任意未使用占位符名称的 consistent-this

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2021-10-17
    • 2016-09-21
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多