]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-extra-bind | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | further_reading: | |
5 | - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind | |
6 | - https://www.smashingmagazine.com/2014/01/understanding-javascript-function-prototype-bind/ | |
7 | --- | |
8 | ||
9 | ||
eb39fafa DC |
10 | |
11 | The `bind()` method is used to create functions with specific `this` values and, optionally, binds arguments to specific values. When used to specify the value of `this`, it's important that the function actually uses `this` in its function body. For example: | |
12 | ||
13 | ```js | |
14 | var boundGetName = (function getName() { | |
15 | return this.name; | |
16 | }).bind({ name: "ESLint" }); | |
17 | ||
18 | console.log(boundGetName()); // "ESLint" | |
19 | ``` | |
20 | ||
21 | This code is an example of a good use of `bind()` for setting the value of `this`. | |
22 | ||
23 | Sometimes during the course of code maintenance, the `this` value is removed from the function body. In that case, you can end up with a call to `bind()` that doesn't accomplish anything: | |
24 | ||
25 | ```js | |
26 | // useless bind | |
27 | var boundGetName = (function getName() { | |
28 | return "ESLint"; | |
29 | }).bind({ name: "ESLint" }); | |
30 | ||
31 | console.log(boundGetName()); // "ESLint" | |
32 | ``` | |
33 | ||
34 | In this code, the reference to `this` has been removed but `bind()` is still used. In this case, the `bind()` is unnecessary overhead (and a performance hit) and can be safely removed. | |
35 | ||
36 | ## Rule Details | |
37 | ||
38 | This rule is aimed at avoiding the unnecessary use of `bind()` and as such will warn whenever an immediately-invoked function expression (IIFE) is using `bind()` and doesn't have an appropriate `this` value. This rule won't flag usage of `bind()` that includes function argument binding. | |
39 | ||
40 | **Note:** Arrow functions can never have their `this` value set using `bind()`. This rule flags all uses of `bind()` with arrow functions as a problem | |
41 | ||
42 | Examples of **incorrect** code for this rule: | |
43 | ||
8f9d1d4d DC |
44 | ::: incorrect |
45 | ||
eb39fafa DC |
46 | ```js |
47 | /*eslint no-extra-bind: "error"*/ | |
48 | /*eslint-env es6*/ | |
49 | ||
50 | var x = function () { | |
51 | foo(); | |
52 | }.bind(bar); | |
53 | ||
54 | var x = (() => { | |
55 | foo(); | |
56 | }).bind(bar); | |
57 | ||
58 | var x = (() => { | |
59 | this.foo(); | |
60 | }).bind(bar); | |
61 | ||
62 | var x = function () { | |
63 | (function () { | |
64 | this.foo(); | |
65 | }()); | |
66 | }.bind(bar); | |
67 | ||
68 | var x = function () { | |
69 | function foo() { | |
70 | this.bar(); | |
71 | } | |
72 | }.bind(baz); | |
73 | ``` | |
74 | ||
8f9d1d4d DC |
75 | ::: |
76 | ||
eb39fafa DC |
77 | Examples of **correct** code for this rule: |
78 | ||
8f9d1d4d DC |
79 | ::: correct |
80 | ||
eb39fafa DC |
81 | ```js |
82 | /*eslint no-extra-bind: "error"*/ | |
83 | ||
84 | var x = function () { | |
85 | this.foo(); | |
86 | }.bind(bar); | |
87 | ||
88 | var x = function (a) { | |
89 | return a + 1; | |
90 | }.bind(foo, bar); | |
91 | ``` | |
92 | ||
8f9d1d4d DC |
93 | ::: |
94 | ||
eb39fafa DC |
95 | ## When Not To Use It |
96 | ||
97 | If you are not concerned about unnecessary calls to `bind()`, you can safely disable this rule. |