Published on 07/30/2018
As of ES6, we have an additional way to write function expressions in our code – as arrow functions. This new syntax offers 3 main benefits:
this is lexically bound, meaning that it is not re-bound when you use an arrow function inside of another function – which is very helpful for asynchronous callbacksLet’s take a look at a basic example to get familiar with the new syntax. Here we’ll take an array of pet names and map over them, using the different function syntax options:
Parentheses around parameters are really only needed when you have more than one parameter. In the above example, parentheses around name is not required. You can keep it if you prefer but it is completely optional. If you have multiple arguments or no arguments at all, however, you do need to pass an empty set of parentheses:
An explicit return is when you explicitly write out the return keyword before the statement. With an implicit return, you do not need to write out the return keyword. You can also remove the curly brackets and pull your statement up to one line.
At the time of writing, arrow functions are always anonymous functions. If you are using an arrow function, you cannot name it. You can, however, assign it to a variable just like you would do with the function expression expression syntax.
One downside to an anonymous function (as opposed to a named function) is that you won’t get as good of an error message, should an error occur in your code. It is often more helpful to know the name of the function where the error occurred, as opposed to just a line number in a stack-trace. That said, I certainly wouldn’t let that discourage you from using them, it’s just something to take note of.
As mentioned above, arrow functions do not define their own this value, rather the this inside of an arrow function refers to the parent’s scope. Therefore, you don’t want to just use arrow functions everywhere because it’s less to type. Instead, you need to know what the benefits and downsides are and use them where applicable. Take the following code, for example:
As you can see here, if you want this to reference the actual element that was clicked, using an arrow function would not be what you want.
On the other hand, as mentioned previously, arrow functions are great for asynchronous code because they automatically bind the context of the this within the callback to the scope in which they’re called. With a traditional function, you would need to explicitly bind this, using something like .bind(), otherwise the callback function will be bound to the global window object when it is returned. This is because the global window object is the default context for anything that is not specifically bound to something else.
It’s also worth noting that, like the this value, arrow functions also do not have their own arguments object. The arguments object within an arrow function is a reference to the arguments object of the enclosing scope. In many cases, however, using rest parameters is a good alternative to working with an arguments object.
In addition to the above caveats with the binding this keyword, arrow functions do not have a prototype property and they cannot be used as constructors (they will throw an error when used with new). Because of this, these types of function expressions are best suited for non-method functions and are not ideal within an object-oriented programming style.
---
Category: JavaScript
Tags: javascript, esnext