Normally, I see function closure achieved by the form
var closedF = (function()
{
return function(){/* return value */}
})();
so that calling closedF()
returns the return value from the inner function. But I want to create closedF
using a function declaration (the above is a function expression) so that it is defined at parse time. I.e.
function closedF()
{
return function(){/* return value */}
}
but this doesn't work because when calling closedF()
, it returns the inner function as opposed to the return value of the inner function. Note: with the above declaration I could use closedF()()
, but that's just inelegant.
Is this possible?
p.s. As is usually the case there are many ways for me to solve my particular programming problem, but I actually want someone to show me that closed-over functions aren't "second-class citizen" functions in JS.