Arrow functions revisited
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) – func is executed by forEach for every array item. - setTimeout(func) – func is 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 info:object-methods, 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: 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: 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.
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: The same without an arrow function would look like: 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 info: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.
let group = {
title: "Our Group",
students: ["John", "Pete", "Alice"],
showList() {
*!*
this.students.forEach(
student => alert(this.title + ': ' + student)
);
*/!*
}
};
group.showList();
Follow the lesson from Microsoft Web-Dev-For-Beginners course