I'm newbie in JS, and in this JS closure example
var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
I can't get why the variable add
is assigned to the invocation of the function, not the function itself.
In other words, now add is supposed to be referencing an invoked function, and to call add, we don't need to add ()
at the end, it is already called.
Why does the example call it like this: add()
? I can't find the exact term for it, but isn't this like 'double calling' the function ?