When to Use Arrow Functions
There are pros and cons of using arrow functions instead of regular or anonymous functions. We discussed the benefits and limitations of arrow functions throughout this chapter. Let’s wrap up this discussion with the answer to the question: when should we consider using arrow functions?
Here are some recommendations that will help you make the right choice:
-
Don’t use arrow functions to define methods of a class, in an object literal, or through Object.prototype. The major hiccup here is the lexical scoping of this. If the method were defined using an arrow function, when the method is called, this will not refer to instance on which the method is called.
-
Avoid multiline arrow functions as arguments to functions—see ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access