简体   繁体   English

ESLint prefer-arrow-callback错误

[英]ESLint prefer-arrow-callback error

I've got a problem with ESLint 我的ESLint有问题

Here is my function: 这是我的功能:

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

And Here is what ESLint tell me: 以下是ESLint告诉我的内容:

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

How to solve this error? 如何解决这个错误?

It is basically saying to use Arrow function syntax in the filter callback function. 它基本上是说在filter回调函数中使用箭头函数语法。

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

Here's what ESLINT documentation for prefer-arrow-callback says 以下是prefer-arrow-callback ESLINT文档

Arrow functions are suited to callbacks, because: 箭头函数适用于回调,因为:

  • this keywords in arrow functions bind to the upper scope's. 箭头函数中的this关键字绑定到上部范围。

  • The notation of the arrow function is shorter than function expression's. 箭头函数的表示法比函数表达式短。

And, in following cases the error will be thrown 并且,在以下情况下,将抛出错误

 /*eslint prefer-arrow-callback: "error"*/ foo(function(a) { return a; }); foo(function() { return this.a; }.bind(this)); 

Your code is same same as the first snippet. 您的代码与第一个代码段相同。 So, the error prefer-arrow-callback is shown by ESLint. 因此,ESLint显示错误prefer-arrow-callback

To solve the error, you can 要解决错误,您可以

  1. use Arrow function syntax(as shown above) 使用箭头函数语法(如上所示)
  2. Suppress the error by using options and named function 使用选项和命名函数来抑制错误

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

解决“ 意外函数表达式。(prefer-allow-callback) ”错误在eslint中在代码的开头写下这段代码:

/*eslint prefer-arrow-callback: 0*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM