What is the purpose of defining the name of a function that is assigned to a variable?

Asked

Viewed 185 times

1

I was taking a look at some posts here on the site and I came across this question here. Well, the accepted answer explained the statement of expression of functions, anonymous, named and self-appointed. Causing my doubt on the named function. What is the function name for if the value of the return is in the variable name and not in the function name. What is the purpose of naming a function that is inside a variable?

//expressão anônima de função
var a = function() {
  return 3;
}

//expressão nominada de função
var a = function bar() {            // dúvida para que serve bar
  return 3;
}

//expressão "autoinvocada" de função
(function digaOi() {
   alert("Oi!");
})();
  • In my view it is a duplicate "Possible" because I believe the linked answer already explains the difference in the part Example of scope visibility and in my understanding instead of opening a question you could comment on in the answer, but it is only my opinion, for it to be considered accurate duplicate of the vote of other users.

  • Yes I saw Example of scope visibility, I still don’t understand it. There it is in the example ** var functionDois = Function banana() { banana() }** right? Just called banana, how do I work with the function banana. I can do it like this: var funcaoDois = Function banana() { banana() { console.log("Something")}. }. That’s what made me doubt.

  • Give a read on the other answers of the same link they complement each other.

  • Interestingly, the same question was asked yesterday by another user. I marked as duplicate of the same question I pointed out in the case of yesterday, let us know if the content there (+ the answers here) do not solve your question.

2 answers

2

As commented on in the question you quoted - and in the @dvd answer, there is the difference between the scopes. When you do:

var a = function bar() {
    // ...
}

Within the function there will be the reference to the function itself bar, something that in the anonymous function will not exist.

var a = function () {

}

How could it be possible to reference something without a name? But why not by the object itself a? Well, function is the very definition of a, which implies that within it the object a is not defined.

And why is defining the name useful? Well, the simplest solution that could use this technique is to define a recursive function as callback of an event:

function myEvent(callback, n) {
  return callback(n);
}

const result = myEvent(function fatorial(n) {
  return (n == 0) ? 1 : n * fatorial(n-1)
}, 5);

console.log(result)

Note: notice that in doing

 
 var a = function bar() {}
 
console.log(a.name)
 

The value of a.name will be bar, nay a.

  • Entendido Anderson.

0


It would be as a matter of scope, where the function bar() is only accessible within a.

The function a receives another name bar which is only visible within a:

var a = function bar() {
  // bar é visível aqui
  return 3;
}
// bar NÃO é visível aqui
a();   //  retorna 3
bar(); // erro: bar is not defined

I do not see much use in creating a second name for the same function and the latter with limited scope.

See in this example whatever call a how much bar the effect is the same:

var x = 0;
var a = function bar(){
   console.log(x);
   // setTimeout(a, 2000);
   // ou
   setTimeout(bar, 2000);
   x++;
}

a();

  • So I still don’t understand it very well. The keys there in the case delimit which function, to or bar? How I use bar in your second example? Can I do var a = Function bar() { bar(){ console.log("Testing")}?

  • I’m editing the answer to make it clearer.

Browser other questions tagged

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