Friday, 3 September 2021

javascript

 

Difference between arrow and normal functions in JavaScript


function myfunction() {    
      // code  
}

If you wanted to invoke one of these functions, like the one in our example, you would have to call it this way:

myfunction();

const  myfunction() = function myfunction() {    

// Code   

}    

 myfunction()();

function myfunction(param1, param2) {    
// Code 
}    
myfunction('Hi Joe', 'Black');

JavaScript arrow functions were introduced in the ES6 version of JavaScript, which appeared in 2015. They are something of an additional version with respect to normal functions. Like anonymous functions, they are nameless. You may assign them to a variable, but the name of the variable will be just that, the name of the variable, not the name of the function.

To declare these functions, you first declare a variable and then, after the possible parameters, you write the set of symbols =>, which are the representation of an arrow. The name of the arrow functions comes precisely from this symbol. After the arrow, the body of the function is placed between braces. Here is an example:

In case we have only one parameter, we can omit the parentheses:

const myfunction = param => {    

// Code  

}    

myfunction();

On the other hand, in case the function only includes one statement, it is possible to omit the braces of the function. In this case, it is understood that there is a return statement, so the result of the statement is returned by the function:


const myfunction = param => 'Hi Joe ' + param;  

myfunction('Black');


As we have seen, the differences in terms of syntax are quite large. However, for now they are not very different in terms of functionality. Moreover, both normal functions and arrow functions can be used as methods of any class using the same syntax.

However there is a rather big difference between the two types of function. We are talking about the treatment that each type of function makes of the this element in the body of the function. Let's propose the following example:


const shoes = {    

     brand: 'Camper',    

     model: 'boots',    

     buy: function() {      

        console.log(`Buy the ${this.brand} ${this.model}`);    

     },    

     stop: () => {      

       console.log(`Stop buying ${this.brand} ${this.model}`);    

     }  

}

However, in the function stop(), the word this does not refer to the object, but refers to the scope in which the constant shoes is defined. That is, the parent context above the object.

In arrow functions, "this" does not refer to the instance of the object in which it is defined, but refers to the scope that this refers to externally. This means that arrow functions are not the best choice when defining a method on an object, since you will usually always want to have access to the object inside the function. In any other case, the use of arrow functions is recommended.

No comments:

Post a Comment

7 Common mistakes in Dot Net — You can avoid

  There are many common mistakes made during .NET (ASP.NET, .NET Core) development, which affect performance, security, and code… Code Crack...