When to not use arrow functions in JavaScript
Arrow functions are a more concise alternative to the traditional function expression but they aren't appropriate everywhere.
Let's check out some examples of times you would not want to use arrow functions.
Scenario 1. Callback functions that require dynamic context.
Callback functions that require dynamic context, such as the element clicked, are not good candidates for arrow functions because you are unable to change the pre-defined context of this
.
Using a regular function allows the value of this
to change based on the context of the target element.
arguments
object.
Scenario 2. Functions that use the Arrow functions do not bind the global arguments
object.
But with a regular function, you have access to the global arguments
object.
Scenario 3. Prototype Methods
The this
value in the sayHello
method references the global object and not the User
object that it is trying to access.
Instead, write your prototype methods the old fashioned way.
Scenario 4. Object methods
The this
value in the sayHello
method references the global object and not the User
object that it is trying to access.
Instead, write your prototype methods the old fashioned way.
Last updated: