Learning about arrow functions
Arrow functions were a new addition to ES6. Basically, they serve two main purposes:
- They shorten the syntax for writing functions.
- They also automatically make the immediate scope parent, the
this
object, the arrow function's parent.
Let me explain this
a bit more before continuing as it's critical knowledge for JavaScript developers.
In JavaScript, the this
object, the owner object instance that member properties and methods belong to, can change based on the context of a call. So, when a function is called directly—for example, MyFunction()
—the parent this
would be the caller of the function; that is to say, the current scope's this
object. For browsers, that would usually be the window
object. However, in JavaScript, functions can also be used as object constructors—for example, new MyFunction()
. In this case, the this
object inside the function would be the object instance...