no-extra-bind
Disallow .bind() on functions that do not use this
.bind(user) sets this inside a function. If the function never reads this, that changes nothing. On an arrow function it has no effect either, because an arrow function takes this from where it's written. The call is usually left over from when the function did use this.
The rule only checks a function written right before the .bind(), and only a call with one argument.
--fix removes the .bind() call if its argument is a name, a literal or this. It leaves .bind(getUser()) alone, because removing it would also remove the call to getUser().
Configure#
Off by default. To turn it on:
[lint.rules]
"no-extra-bind" = "error" # or "warn"Examples#
const greet = function () {
return "Hello";
}.bindThe function binding is unnecessary.(user);
const log = (() => {
console.log(this.name);
}).bindThe function binding is unnecessary.(user);A this inside a nested function belongs to that function, not to the outer one:
const setup = function () {
button.onclick = function () {
this.disabled = true;
};
}.bindThe function binding is unnecessary.(form);const greet = function () {
return `Hello ${this.name}`;
}.bind(user);
const save = user.save.bind(user);Extra arguments fill in parameters, which is useful even without this:
const addFive = function (a, b) {
return a + b;
}.bind(null, 5);