最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ESLint prefer-arrow-callback error - Stack Overflow

programmeradmin1浏览0评论

I've got a problem with 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:

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

How to solve this error?

I've got a problem with 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:

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

How to solve this error?

Share edited Jul 6, 2016 at 11:21 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Jul 6, 2016 at 10:26 StanouuStanouu 1301 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It is basically saying to use Arrow function syntax in the filter callback function.

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

Here's what ESLINT documentation for prefer-arrow-callback says

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.

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.

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() {});
    

to solve "unexpected function expression. (prefer-allow-callback)" error in eslint write this code in the beginning of your code:

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

发布评论

评论列表(0)

  1. 暂无评论