|
| 1 | +# Arrow functions revisited |
| 2 | + |
| 3 | +Let's revisit arrow functions. |
| 4 | + |
| 5 | +[cut] |
| 6 | + |
| 7 | +Arrow functions are not just a "shorthand" for writing small stuff. |
| 8 | + |
| 9 | +Javascript is full of situations where we need to write a small function, that's executed somewhere else. |
| 10 | + |
| 11 | +For instance: |
| 12 | + |
| 13 | +- `arr.forEach(func)` -- `func` is executed by `forEach` for every array item. |
| 14 | +- `setTimeout(func)` -- `func` is executed by the built-in scheduler. |
| 15 | +- ...there are more. |
| 16 | + |
| 17 | +It's very in the spirit of Javascript to create a function and pass it somewhere. |
| 18 | + |
| 19 | +And in such functions we usually don't want to leave the current context. |
| 20 | + |
| 21 | +## Arrow functions have no "this" |
| 22 | + |
| 23 | +As we remember from the chapter <info:object-methods>, arrow functions do not have `this`. If `this` is accessed, it is taken from the outside. |
| 24 | + |
| 25 | +For instance, we can use it to iterate inside an object method: |
| 26 | + |
| 27 | +```js run |
| 28 | +let group = { |
| 29 | + title: "Our Group", |
| 30 | + students: ["John", "Pete", "Alice"], |
| 31 | + |
| 32 | + showList() { |
| 33 | +*!* |
| 34 | + this.students.forEach( |
| 35 | + student => alert(this.title + ': ' + student) |
| 36 | + ); |
| 37 | +*/!* |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +group.showList(); |
| 42 | +``` |
| 43 | + |
| 44 | +Here in `forEach`, the arrow function is used, so `this.title` in it is exactly the same as in the outer method `showList`. That is: `group.title`. |
| 45 | + |
| 46 | +If we used a "regular" function, there would be an error: |
| 47 | + |
| 48 | +```js run |
| 49 | +let group = { |
| 50 | + title: "Our Group", |
| 51 | + students: ["John", "Pete", "Alice"], |
| 52 | + |
| 53 | + showList() { |
| 54 | +*!* |
| 55 | + this.students.forEach(function(student) { |
| 56 | + // Error: Cannot read property 'title' of undefined |
| 57 | + alert(this.title + ': ' + student) |
| 58 | + }); |
| 59 | +*/!* |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +group.showList(); |
| 64 | +``` |
| 65 | + |
| 66 | +The error occurs because `forEach` runs functions with `this=undefined` by default, so the attempt to access `undefined.title` is made. |
| 67 | + |
| 68 | +That doesn't affect arrow functions, because they just don't have `this`. |
| 69 | + |
| 70 | +```warn header="Arrow functions can't run with `new`" |
| 71 | +Not having `this` naturally means another limitation: arrow functions can't be used as constructors. They can't be called with `new`. |
| 72 | +``` |
| 73 | +
|
| 74 | +```smart header="Arrow functions VS bind" |
| 75 | +There's a subtle difference between an arrow function `=>` and a regular function called with `.bind(this)`: |
| 76 | +
|
| 77 | +- `.bind(this)` creates a "bound version" of the function. |
| 78 | +- The arrow `=>` doesn't create any binding. The function simply doesn't have `this`. The lookup of `this` is made exactly the same way as a regular variable search: in the outer lexical environment. |
| 79 | +``` |
| 80 | + |
| 81 | +## Arrows have no "arguments" |
| 82 | + |
| 83 | +Arrow functions also have no `arguments` variable. |
| 84 | + |
| 85 | +That's great for decorators, when we need to forward a call with the current `this` and `arguments`. |
| 86 | + |
| 87 | +For instance, `defer(f, ms)` gets a function and returns a wrapper around it that delays the call by `ms` milliseconds: |
| 88 | + |
| 89 | +```js run |
| 90 | +function defer(f, ms) { |
| 91 | + return function() { |
| 92 | + setTimeout(() => f.apply(this, arguments), ms) |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +function sayHi(who) { |
| 97 | + alert('Hello, ' + who); |
| 98 | +} |
| 99 | + |
| 100 | +let sayHiDeferred = defer(sayHi, 2000); |
| 101 | +sayHiDeferred("John"); // Hello, John after 2 seconds |
| 102 | +``` |
| 103 | + |
| 104 | +The same without an arrow function would look like: |
| 105 | + |
| 106 | +Аналогичная реализация без функции-стрелки выглядела бы так: |
| 107 | + |
| 108 | +```js |
| 109 | +function defer(f, ms) { |
| 110 | + return function(...args) { |
| 111 | + let ctx = this; |
| 112 | + setTimeout(function() { |
| 113 | + return f.apply(ctx, args); |
| 114 | + }, ms); |
| 115 | + }; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +Here we had to create additional variables `args` and `ctx` so that the function inside `setTimeout` could take them. |
| 120 | + |
| 121 | +## Summary |
| 122 | + |
| 123 | +Arrow functions: |
| 124 | + |
| 125 | +- Do not have `this`. |
| 126 | +- Do not have `arguments`. |
| 127 | +- Can't be called with `new`. |
| 128 | +- (They also don't have `super`, but we didn't study it. Will be in the chapter <info:class-inheritance>). |
| 129 | + |
| 130 | +That's because they are meant for short pieces of code that does not have the own "context", but rather works in the current one. And they really shine in that use case. |
0 commit comments