no-extra-bind

禁止对 .bind() 进行不必要的调用

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

bind() 方法用于创建具有特定 this 值的函数,并且可以选择将参数绑定到特定值。当用于指定 this 的值时,函数在其函数体中实际使用 this 很重要。例如:

var boundGetName = (function getName() {
    return this.name;
}).bind({ name: "ESLint" });

console.log(boundGetName());      // "ESLint"

这段代码很好地使用了 bind() 来设置 this 的值。

有时在代码维护过程中,this 值会从函数体中移除。在这种情况下,您最终可能会调用 bind() 却什么也没做:

// useless bind
var boundGetName = (function getName() {
    return "ESLint";
}).bind({ name: "ESLint" });

console.log(boundGetName());      // "ESLint"

RAWPIQwT9V6GsTU+5ZRUvX8tQVJUeJ+H8XQrBhnhMcjsxLrNPhoe7gCEhc/ck0OrvSvuTVfq2ytKHBgeHhT8R4k3YUC/aR1SwY3lodEf5dwFyUPJkRnqDiMabkihm4H5Zi9jqiBI2YpTpfO7ksoox6wSzzMcLaDLGXlavQ9I8w1tru+1Rkw09oEsnwy6bL8KBYP2vut4rNLkpzp8I6sQ7kOSHQfPjE0VFKcjNBew20x35dvwvMOKo3ax71ac6zvHwchPtmJJoD9+Rb/RJA57Ig==

规则详情

aTHL4/zY41cMNcs0s5OnNOFMFX4aH4sktiUmDkD1cH6mJ07ESLOyC/x4ZkNUw+Swvr2GxfOV9f69Vx0++upbdaqPn7Hs3U4RTTXC2LWhTu8sXqlVWZmJO01jQiFKFn/hwBPYKmM5mYoK8vyulEmH5cvUorxYK4r96u7NpGA7vh+J3s3TCNM+x13cRZlktxNIzIJv3NvW0A0ZBxZPf1Hy/+F5A9OalEidOhAwCQD1XBtvmpjIevG8bvCL/0SDLG4K7w8PnyBjhlMvv+W7oRR33QMKdVDTJ7ByxR898QXYG8tlD4RGsAfZNqJgQrO1EroeMo7qNSfALNItwilRZ30FbBMZw0ESewhpNHmU85pYuENEVwdbBlbqN/bjnQkcpsyzp27PR24jPElqTAw3HWO+Kw==

Mhb8NolybLNpharp2XJ8HAjHCaScejiNW+OmMzn1v1jHnteHCSlVY6eMf7s47MN2PY2rqkx3pkRwKO+Dw1lG2JLUf/TOAl+bwyn/RBIySxALl03eQBzVWTNuEyWsX3DeCqmjVZPAC2AP9jJRnSe9S7ZKLeKXztKTvYo/q1YVzGPPQxVIyXAh/9AjxgISOcsKe5x3jHlHqdq5t/3SL8HyTzd6Ow4Se1QuPsOcC3PcqjzawSH+hl7mkZLvn3U4h+xdgykEjRGf9Hewj/YGN+PwhA==

QnwhPm/cyTBJK+3dwgoJPe3E20un+2KP2CiFhQ+78TgFiUScrcmb3urZ1LYB2VeR

/*eslint no-extra-bind: "error"*/
/*eslint-env es6*/

var x = function () {
    foo();
}.bind(bar);

var x = (() => {
    foo();
}).bind(bar);

var x = (() => {
    this.foo();
}).bind(bar);

var x = function () {
    (function () {
      this.foo();
    }());
}.bind(bar);

var x = function () {
    function foo() {
      this.bar();
    }
}.bind(baz);

ZV3WTnsA6mjxcovKoGG8hTv7+Ha1z2USrMCyxcmIv+4859h0MFN8WvPgRtljmRMU

/*eslint no-extra-bind: "error"*/

var x = function () {
    this.foo();
}.bind(bar);

var x = function (a) {
    return a + 1;
}.bind(foo, bar);

何时不使用

6W9+8cLRDxq9CyafbVIRiSK7AqvYlHvGrhrq6/tK0IWR7OY7aTkUCSaOJzIUQ/03AEP1Mqzt9iRMG2GfHIfVv0c/v5CtAVJ9IQzWzUcGnSfFefCSyDz3/Xjt2eNzLD1/KPd5vy9EqrvSyMGY1Uu8rA==