javascript - ESLint prefer-arrow-callback 错误

标签 javascript ecmascript-6 eslint

我遇到了 ESLint 的问题

这是我的功能:

$productItem.filter(function (i, el) {
        return el.getBoundingClientRect().top < evt.clientY
    }).last()
    .after($productItemFull)

这是 ESLint 告诉我的:

warning  Missing function expression name  func-names
error    Unexpected function expression    prefer-arrow-callback

如何解决这个错误?

最佳答案

基本上就是说用Arrow function filter 回调函数中的语法。

$productItem.filter((i, el) => el.getBoundingClientRect().top < evt.clientY)
    //              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    .last()
    .after($productItemFull);

这是ESLINT documentation for prefer-arrow-callback

Arrow functions are suited to callbacks, because:

  • this keywords in arrow functions bind to the upper scope’s.

  • The notation of the arrow function is shorter than function expression’s.

并且,在以下情况下会抛出错误

/*eslint prefer-arrow-callback: "error"*/

foo(function(a) { return a; });
foo(function() { return this.a; }.bind(this));

您的代码与第一个代码段相同。因此,错误 prefer-arrow-callback 由 ESLint 显示。

要解决这个错误,你可以

  1. 使用 Arrow function语法(如上图)

  2. 使用 options 抑制错误和命名函数

    /*eslint prefer-arrow-callback: ["error", { "allowNamedFunctions": true }]*/
    
    foo(function bar() {});
    

关于javascript - ESLint prefer-arrow-callback 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221760/

相关文章:

webpack - 如何让 eslint 导入/首先理解我的 webpack 别名?

javascript - 通过 css 居中 msform

javascript - Store 没有带有 mergeReducer 的有效 reducer

javascript - 如果我可以通过这个调用父方法那么为什么要在 ES6 中使用 super 呢?

webpack - 将 eslint-webpack-plugin 添加到项目中以提供 Typescript linting

node.js - Nodemon '' npm' 未被识别为内部或外部命令

javascript - 无法注册 Bower 包 : EINVFORMAT

javascript,promises,如何在 then 范围内访问变量 this

javascript - 为移动设备提供与桌面设备不同的内容

javascript - webpack -p 禁用我的 ejs 模板中的 es2015 功能