What is the correct way to declare a javascript Function?

Asked

Viewed 752 times

7

For some time now, I have been observing the use of two forms of function javascript, being the following:

- Form 1:

function soma1(val1, val2){
    return val1 + val2;
};

- Form 2:

var soma2 = function(val1, val2){
    return val1 + val2;
};

The two forms seem to work equally.

My doubts are as follows:

  1. There’s some right or wrong way?
  2. Is there any way considered standard?
  3. Are there differences between the two forms? (from: performance, execution context, etc)
  4. Is there any recommendation for use?
  • @bfavaretto, although it is quite illuminating, the answers of the other question (marked as duplicate), they do not completely answer the doubts of this (in my opinion), since it is different from the other, questions, if there is correct form, if there is pattern, and which recommendations for use, what the answers of the other question clearly answer this is the difference between the two ways of declaring function. I’ll post it to reopen and see what the community considers.

  • 3

    Fernando, it is because there are very few differences: changes the moment when the function is available within the scope, and has the fact that the former requires the function to be named. Only this, the rest is a matter of use, there is no correct form. I still find duplicate, even if the answers there need some complement (I will try to complement mine in the next few days).

1 answer

3


There is a difference between them and so it is not possible to say that there is a correct form. Each one has a purpose.

The preference should be for the first form, it can be considered the default. This is the normal function statement. And it can be slightly faster depending on the implementation of Javascript. But I’ve seen obvious and intuitive things work backwards.

The second way is not creating a traditional function. It is creating what is called anonymous function. You associate the code written on it with a variable and it can be transferred to other parts of the application providing great flexibility when it is required.

In this form the function only exists logically (is in scope) while having a variable sustaining it.

She has an additional advantage that she can function as a enclosure. That is, it can store a value obtained through a variable that was declared in the scope of the function that created this anonymous function. In other words, it holds this value within the anonymous function and can use it as long as it is "alive".

An anonymous function can be returned from the creative function as a result of it. It can also be passed as an argument to another function that expects a code to execute. Another use is as a value of a member of an object. It is rarely useful if it is not passed on to other points of application in some way. Normal function can also do this but if programming within the standards it is more rare to be useful to do this.

In questions linked there is more information about the operation of the second since this is not the focus of this question.

Example of anonymous function usage:

function criaFuncao() {
   return function() { console.log("oi"); };
}
var codigo = criaFuncao();
codigo(); // vai imprimir oi

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.