Friday 2 December 2016

web - Javascript: What is the benefit of two sets of parentheses after function call


Can someone please explain the reason and benefit why you might want to do this in your code?





There's no reason to do it when you'll always do it whenever calling the function (a in your case). The reason in the general case is that the author of a is allowing for the possibility you may not want to call the resulting function right away. So the a()(); case is just a special case of the general case of



var f = a();
// later...
f();




Also, is the function that returns 'hello' considered a closure?




Yes, technically, although in your example it doesn't have anything special to close over that a doesn't already close over, since there are no arguments to a or variables within a.



More (on my anemic blog): Closures are not complicated

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...