Arrow function

Arrow function

Syntax

Arrow function or so-called “fat arrow” is a new syntax for writing JavaScript functions. The most important feature of the arrow function is its shorter syntax, which implies better visibility. Depending on the number of parameters and the body of the function, the syntax has several different ways of writing the code:.

arrow function

a) Syntax appearance when the function body has several expressions

In this case, the syntax of the arrow function is the most similar to an ordinary function, and the only difference is obtained by removing the keyword “function” and adding “fat arrow”. This syntax is used when there are multiple expressions in the function body:

b) Syntax layout when the function body has one expression

If the body has only one expression that is returned with “return” then we can drop both the curly braces and the reserved word “return”:

Example

c) Appearance of the syntax when no parameters are passed to the function

If no parameters are passed to the function then there must be empty parentheses () in the section reserved for function parameters

d) Appearance of the syntax when only one parameter is passed to the function

If only one parameter is passed to the function, then we can remove the brackets around the parameters:

Example

The benefit of the new syntax is even better seen when we use the new methods for iterating over arrays:

Example

e) Syntax layout when returning “Object Literal”

Returning an object literal with ES5 syntax looks like this:

According to the current rules, the previous task with the arrow function would look like this:

However, this syntax is not good, and the function returns undefined, because the parser parses the curly braces as the body of the function and not as an object literal. Therefore, according to him, the syntax with a colon in the body of the function is not good. In order for the parser to recognize that it is ES6 syntax, it is necessary to put ordinary brackets around the object literal:

Keyword domain “this”

arrow function

Arrow function instead of applying one of the 4 standard rules for binding this, it completely bypasses the standard mechanism and binds this to the surrounding scope of visibility (lexical visibility scope). When calling these functions, this inherits the meaning of this from the surrounding function. Simplified this arrow function equates to what this would point to from the external function surrounding the arrow function (it has a built-in engine that practically replaces the known mechanism: “self=this”). When using the arrow function it doesn’t matter how and where the function is called, only what this from the external function points to.

NOTE:
Meaningobtained by the this operator inside the arrow function cannot be changed later! So it cannot be overridden with the predefined bind(), apply() or call() methods or even with the “new” operator.

Example – Problem with closure

There is a known problem when the clousure function inside an object “loses” the reference to that object: :

The reserved word “this” in the clousure function mergeNames() has lost its reference to the person object and points to a global object. This problem can be solved by using the arrow function, because its meaning of the reserved word “this” will be equated to the meaning of this from the surrounding function. In this case the surrounding function is fullName(), and it points to a person object. So this inside the arrow function will also point to the object “person”.

When to use the arrow function?

Arrow function should not always be used ie. as a total replacement for the classic function declaration. There are times when the arrow function shines brightly, but there are also times when it simply cannot be used.

Recommendation for using the arrow function

Generally, it is recommended that the arrow function be used when we have simple functions composed of one statement, where the only statement is return or some calculated value and “this” is not used in the body of the function, unless it is a closure.

Example

Cases when you should avoid the arrow function

Arrow function should not be used:

  • a) If the reserved word “this” is used within the object method (this statement does not apply to closure, because then it is preferable to use the arrow function), because “this” then points to a global object
  • b) If it is a dynamic context, such as a callback function within an EventListener, because “this” does not point to the object to which the listener is attached, but to the global object:
  • c) Within the constructor function, because “this” should point to the newly generated object:
  • d) When the function requires an “arguments” object, because when we use the arrow function we do not have access to the “arguments” object.