why assign the same name to both a variable and a function?

1
const Hola = function Hola() {
   _classCallCheck(this, Hola);
}; 
    
asked by Cristian Camilo Flórez 27.01.2018 в 09:26
source

1 answer

5

That kind of syntax is calling Function Expression or expression of function.

There is no difference between the declaration of a function and an expression of function since in the end both provide the reference of the function in question:

// expresion de funcion
var a = function a(){
  console.log("imprimiendo a");
}

function b(){
  console.log("imprimiendo b");
}

a();
b();

One of the reasons why a function expression would be used to identify the function to which the variable is referring. For example, imagine the following case:

var expresion = function a(){
      console.log("funcion de expresion");
}


var anonima = function(){
  console.log("funcion anonima");
}


debugger;

If you go to the development console you will see how to put the mouse on the variable expresion , you can identify the reference and exact name of the function it contains that is a :

But the same thing does not happen with the anonymous variable:

Note how you identify it with the name of the variable since it is anominated. If in the future the reference of the function were edited to another anonymous function, there would be no way of knowing which is the origin of the function.

Then, answering your question about why the same name, you should ask the developer because it does not make sense. The variable is declared constant so it would have been better to declare a function in a standardized way.

    
answered by 27.01.2018 / 14:44
source