Letās revisit arrow functions.
Arrow functions are not just a āshorthandā for writing small stuff. They have some very specific and useful features.
JavaScript is full of situations where we need to write a small function thatās executed somewhere else.
For instance:
arr.forEach(func)āfuncis executed byforEachfor every array item.setTimeout(func)āfuncis executed by the built-in scheduler.- ā¦there are more.
Itās in the very spirit of JavaScript to create a function and pass it somewhere.
And in such functions we usually donāt want to leave the current context. Thatās where arrow functions come in handy.
Arrow functions have no āthisā
As we remember from the chapter Object methods, "this", arrow functions do not have this. If this is accessed, it is taken from the outside.
For instance, we can use it to iterate inside an object method:
let group = {
title: "Our Group",
students: ["John", "Pete", "Alice"],
showList() {
this.students.forEach(
student => alert(this.title + ': ' + student)
);
}
};
group.showList();
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.
If we used a āregularā function, there would be an error:
let group = {
title: "Our Group",
students: ["John", "Pete", "Alice"],
showList() {
this.students.forEach(function(student) {
// Error: Cannot read property 'title' of undefined
alert(this.title + ': ' + student);
});
}
};
group.showList();
The error occurs because forEach runs functions with this=undefined by default, so the attempt to access undefined.title is made.
That doesnāt affect arrow functions, because they just donāt have this.
newNot having this naturally means another limitation: arrow functions canāt be used as constructors. They canāt be called with new.
Thereās a subtle difference between an arrow function => and a regular function called with .bind(this):
.bind(this)creates a ābound versionā of the function.- The arrow
=>doesnāt create any binding. The function simply doesnāt havethis. The lookup ofthisis made exactly the same way as a regular variable search: in the outer lexical environment.
Arrows have no āargumentsā
Arrow functions also have no arguments variable.
Thatās great for decorators, when we need to forward a call with the current this and arguments.
For instance, defer(f, ms) gets a function and returns a wrapper around it that delays the call by ms milliseconds:
function defer(f, ms) {
return function() {
setTimeout(() => f.apply(this, arguments), ms);
};
}
function sayHi(who) {
alert('Hello, ' + who);
}
let sayHiDeferred = defer(sayHi, 2000);
sayHiDeferred("John"); // Hello, John after 2 seconds
The same without an arrow function would look like:
function defer(f, ms) {
return function(...args) {
let ctx = this;
setTimeout(function() {
return f.apply(ctx, args);
}, ms);
};
}
Here we had to create additional variables args and ctx so that the function inside setTimeout could take them.
Summary
Arrow functions:
- Do not have
this - Do not have
arguments - Canāt be called with
new - They also donāt have
super, but we didnāt study it yet. We will on the chapter Class inheritance
Thatās because they are meant for short pieces of code that do not have their own ācontextā, but rather work in the current one. And they really shine in that use case.
Comments
<code>tag, for several lines ā wrap them in<pre>tag, for more than 10 lines ā use a sandbox (plnkr, jsbin, codepenā¦)