โก๏ธFat Arrow Syntax
Arrow function expressions
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can't be used in all situations.
Differences & Limitations:
Does not have
new.targetkeyword.Can not be used as constructors.
Can not use
yield, within its body.
Let's decompose a "traditional anonymous function" down to the simplest "arrow function" step-by-step:
Note: Each step along the way is a valid "arrow function".
Copy to Clipboard
The { braces } and ( parentheses ) and "return" are required in some cases.
For example, if you have multiple arguments or no arguments, you'll need to re-introduce parentheses around the arguments:
Copy to Clipboard
Likewise, if the body requires additional lines of processing, you'll need to re-introduce braces PLUS the "return" (arrow functions do not magically guess what or when you want to "return"):
Copy to Clipboard
And finally, for named functions we treat arrow expressions like variables:
Copy to Clipboard
One param. With simple expression return is not needed:
Copy to Clipboard
Multiple params require parentheses. With simple expression return is not needed:
Copy to Clipboard
Multiline statements require body braces and return:
Copy to Clipboard
Multiple params require parentheses. Multiline statements require body braces and return:
Copy to Clipboard
To return an object literal expression requires parentheses around expression:
Copy to Clipboard
Rest parameters are supported:
Copy to Clipboard
Default parameters are supported:
Copy to Clipboard
Destructuring within params supported:
Copy to Clipboard
As stated previously, arrow function expressions are best suited for non-method functions. Let's see what happens when we try to use them as methods:
Copy to Clipboard
Arrow functions do not have their own this. Another example involving Object.defineProperty():
Copy to Clipboard
The call, apply and bind methods are NOT suitable for Arrow functions -- as they were designed to allow methods to execute within different scopes -- because Arrow functions establish "this" based on the scope the Arrow function is defined within.
For example call, apply and bind work as expected with Traditional functions, because we establish the scope for each of the methods:
Copy to Clipboard
With Arrow functions, since our add function is essentially created on the window (global) scope, it will assume this is the window.
Copy to Clipboard
Perhaps the greatest benefit of using Arrow functions is with DOM-level methods (setTimeout, setInterval, addEventListener) that usually required some kind of closure, call, apply or bind to ensure the function executed in the proper scope.
Traditional Example:
Copy to Clipboard
Arrow Example:
Copy to Clipboard
Arrow functions do not have their own arguments object. Thus, in this example, arguments is a reference to the arguments of the enclosing scope:
Copy to Clipboard
In most cases, using rest parameters is a good alternative to using an arguments object.
Copy to Clipboard
Arrow functions cannot be used as constructors and will throw an error when used with new.
Copy to Clipboard
Arrow functions do not have a prototype property.
Copy to Clipboard
The yield keyword may not be used in an arrow function's body (except when permitted within functions further nested within it). As a consequence, arrow functions cannot be used as generators.
Arrow functions can have either a "concise body" or the usual "block body".
In a concise body, only an expression is specified, which becomes the implicit return value. In a block body, you must use an explicit return statement.
Copy to Clipboard
Keep in mind that returning object literals using the concise body syntax params => {object:literal} will not work as expected.
Copy to Clipboard
This is because the code inside braces ({}) is parsed as a sequence of statements (i.e. foo is treated like a label, not a key in an object literal).
You must wrap the object literal in parentheses:
Last updated
Was this helpful?