Define closed-over function at parse time?
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 declarat开发者_如何学JAVAion 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.
Your problem has not necessarily anything to do with function expression vs function declaration.
In the both cases you showed, you don't get the return value of the inner function.
You could get it by just calling the inner function and return it's value:
function closedF() {
return (function(){
/* return value */
}()); // <- calling the inner function here
}
The disadvantage is that the inner function will always be defined anew when calling the outer function. You could prevent this by caching the inner function as a property of the outer function:
function closedF() {
var func = closedF.__func || (closedF.__func = function(){
/* return value */
});
return func();
}
However, this might be more confusing than helpful.
Easier is to execute the outer function immediately and assign the returned function to a variable, as @James Long shows in his answer.
Update:
You could also do this:
(function() {
window.closedF = function() {
};
}());
though both are function expressions
This isn't possible. The only way to get a function from within a closure is to actually execute the wrapping function which, of course, can't happen during compile.
The code you're actually looking for is:
var closedF = function(){
return function(){
// do stuff
}
}(); // <-- note the (), which immediately calls the outer function
Ultimately, there isn't even a valid use-case for what you're asking for. The point of a closure is that you can get references to external variables passed-in during program execution that are available to a specific function. If you're trying to do this during compile, you already have access to anything you're trying to get since no coe has been executed yet. Therefore there is no need for a closure.
If you post exactly what you're trying to do, there may be another solution.
Did you consider passing a function as a parameter?
function closedF(fn){
return fn;
}
And to use it:
var fnParam = function(){...};
...
closedF(fnParam);
You mean something like this:
function closedF() {
return function () {
}
}
Yup, that's perfectly acceptable
Edit: Reading the comments, I think you need a self-executing anonymous function:
var closedF = (function () {
return function () {
};
}());
This will create a function that can simply be executed by calling closedF()
, but I don't know if it's possible as a declaration.
精彩评论