Based on the difference between function names with parentheses and without parentheses when calling functions in js

  • 2021-07-06 09:48:55
  • OfStack

Sample code:


<span style="font-size:18px;">function hi(){ 
 var a = 1; 
 return function(){
 console.log(a++);
 }; 
};   
var aaa = hi();
var bbb = hi;
</span>

As in the above code:

aaa is an anonymous function that assigns the running result of hi () to it, that is, return returns. If there is a closure, the same a will be accessed every time aaa is called. The running result of aaa () is 1 for the first time and 2 for the second time

bbb will assign the function name hi to it, and then call bbb () and return a function expression, that is, function () {console. log (a + +)};


Related articles: