prefer-arrow-callback

需要使用箭头函数进行回调

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

箭头函数可以成为回调或函数参数的函数表达式的有吸引力的替代方案。

例如,箭头函数会自动绑定到它们周围的范围/上下文。这为显式绑定函数表达式的 ES6 之前的标准提供了一种替代方法,以实现类似的行为。

此外,箭头函数是:

    kh8ASf9PuOvPo3F2AYqWC0yRZMRsLcuwHzT6fzPCFEp2sPmlvswxpaKQpiU51QxMM+DQthDwxlWsc/FxnHAnfmeq23KbY2srR5MpX+tR+7FnkUQXivzPmer/is4WRL81M0mmV/4B3t1X3Wg1uU+3NurC2WhLIHopW43ZevI4C8iBcnLD/ZIu+He5Opx55qBq

规则详情

MiRwn+sovmV8nuwY9B5KNrH6rhcWHuKiqwlQt8GkXVMyb8jfWiSiw2DfXrvRPkca0cVtimZVH1ox+sy/7c8B2ngyxxoYqbeyKtQ9cNjG3nneO+EwyjqcOnyPRQ1j52kOA3J6OIfaxdHaM4Gb0r5xMZuho+gPCKJW58Rh7mBm9WDkH/KlCU4h35bcOlJf5TVNmmE0/gfPm2c8RW/sUbkA0A==

XInn5d9yV11P7yxLZrnqGQSngAo3YfTKJ3NsTwh3Mms=

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

foo(function(a) { return a; }); // ERROR
// prefer: foo(a => a)

foo(function() { return this.a; }.bind(this)); // ERROR
// prefer: foo(() => this.a)

TGjvS06gd86LvWqOLJpaOLehrIdScQbujODhpQB9Iv6+uUUU8HrW8p/kLMxtuLQHZ+OO4dehAd8FouyzNSlOZg==

Es5Htr8s1c6mpEThG8Vm8NsTi+WDTKUsHH9uAYXXnKCGxSTOqkJMHdcrUtJkAQVO

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

// arrow function callback
foo(a => a); // OK

// generator as callback
foo(function*() { yield; }); // OK

// function expression not used as callback or function argument
var foo = function foo(a) { return a; }; // OK

// unbound function expression callback
foo(function() { return this.a; }); // OK

// recursive named function callback
foo(function bar(n) { return n && n + bar(n - 1); }); // OK

选项

i3/w9NUiEx5nH0FzbeKgkHWjybt86txVDcAknWC6hhovIGzCyhZ40c5rbqlqo0GzGqiWPA4+69GmGDT2Gqj6UQ==

NQjAmJ4mvdY2iq+B7kQqZzzXlD6s2ImE8+vjt1YYib7BNhOb7sxzm7ptT+0MLNj59wMW0I4a6GcMA0bV2XlquTvG3CmU6DZ3yCV9h4TxnCM=

allowNamedFunctions

XWAsChzp9/5rGnf0aqOafCBoctq+zHS5dT/nuS+rLTtHNNzNHP+aRj62K9bd6D/qk6kAbQQg0KI3dOQQEa8QdSMhH0zAF8Uxuld/5qe8WtMY8krpaZsfU3tca7zuwuqmCXoTsmfxTEoDAvpJhMaYRfCXzT2WUYlTgMgK0mYTfA1fbl9Yt2hqCa8zs9Pco/PNfMHnFbKWb93D9iox3kWjag==

hq+BMxaBKbTfZ9XzNMJ3H4FoRlGJ2OL88vJu6WInwK1mfQSmEVnYlDC8EC2UpQcqhdcMjAaq+CD6Q4oOkfJwmFtbAoELrAWgJgP+ZVoJLoxmP4siLehnh/y08NOPfwRXz5qBGNct6gwBzJVMjwEfi7rw9xw+s0GnFnTximGNNZ8=

uP9t5kJoDoa8e3yNX3IvRCVScpfrvFl3CAv+THYQlVup2hZDzcirqk3PTC/qBRgKbFB6/M6G7RP/jUl7ERaYEC7fx4cuXdLv7l3waI2dBoY=

/* eslint prefer-arrow-callback: [ "error", { "allowNamedFunctions": true } ] */

foo(function bar() {});

allowUnboundThis

XWAsChzp9/5rGnf0aqOafHvSR8D/JHZIpOTR+0pxZWXMtbv7QdqGekvlgYMGuHDxsMHq5jGP7So6kBahW/HhyP2+R7SeQhxar6WvK7cBgkBaBC+FZW0ObVO81zWfUmLW7fY/ceOTagXJDZDIQH0XJIK941it8r2VD/MO6RfKsi6rzZpn7yerRezWMVW0XAdse1bJM9ECcRiG/dbHyd7qtmqhViL5PHI4+Zb7nOfkMYwZkJXCG4OClaKJtMr0CS0OEQA+hfSgzacK59bVkIdlLQ==

UUrvPtQvaaijHlVzTnKf02qyIpUWrMDRzG0lxowSVvM6cAhHPhCRiRWgR55P7xZ+LctRJ3aMjwUIPl9Ea5ncvyolICKbIMDacNF5RIK8AwCpP63qelorG03qhJUsTX4CkgK8t2bGy21oUHVST2e0pIy1mdCbdeHzLVbAXQSPlMM=

9nlENak2zAI13XZJMr/SH3IANdNdjbnJkJv0XwerguoItgxwmYZQr6a6LOS8pI7tYm7DbXXKRtrSAHcbH4zEsFMDSQn7fsDOOXudNHYgc6g=

/* eslint prefer-arrow-callback: [ "error", { "allowUnboundThis": false } ] */
/* eslint-env es6 */

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

foo(function() { (() => this); });

someArray.map(function(item) { return this.doSomething(item); }, someObject);

何时不使用

    Uc5HFKRQs5Lw7mF10mBhyYt/dRJw5P+gxr5Ma+xI9iXHKqSvw89cIvQJRhRX3P2IpU5MXdN/zDs2cBHlunZF9BtAvZoknzdePwyjyzF9IJnvM0tQ4BO26/YK+jeINEunsNu1dFuvwdiT1HQmgUVdUiMbEItMSnBvDC/jw1KxqrHdWfaKuMJi31q96sLbtbUUW+NodVYG1jRMVgTQbGszE3kewGfa4hRwaQNGdR996kA=