问题
I have the name of a function in a variable, but everything is within a closure. With this variable I want to call the function, something like this
(function(func) {
this[func]() ; // doesn't work
function bar() {}
function foo() {}
})('bar') ;
Is something like this possible or should I, for example, add the functions to a variable, like
(function(func) {
var ns = {
bar: function() {},
foo: function() {}
};
ns[func]() ; // OK
})('bar') ;
回答1:
Variables and functions declared in the current lexical scope cannot be accessed by name using []
syntax - only property keys (per your second example) can be looked up dynamically based on the contents of another variable.
The only way around this is to resort to eval
, which is almost never a good idea.
An exception applies for variables and functions declared in the global scope - those are actually properties of window
.
回答2:
In my experience, putting a function inside a closure is one way of declaring it "private" - such that nothing outside can access it. Furthermore, it's worth looking at the way code is minified:
Before:
(function() {
function privateFn(var1, var2) {
//TODO
}
return {
publicFn: function() { }
}
})();
After:
(function() {
function _a(_0, _1) {
}
return {
publicFn: function() { }
}
})();
Notice how privateFn
doesn't really exist anymore? The minifier knows, by definition of the language, that nothing can access that function outside - by name, or otherwise. You seem to want to make one of your functions public.
回答3:
Something like this:
(new function() {
this.a = function(x) {
document.getElementById('out').innerHTML += x;
}
})['a']('boom.');
http://jsfiddle.net/CT4du/1/
A closure keeps everything private. And normally, this
in a closure just refers to the window
. But, you can "publicize" items in a "closure" without polluting the global space by turning it into an anonymous object first. This allows you to make one and only one method call on the object before it vanishes ...
You can do normal object things, like use private, lexical variables and expose only the methods you want exposed.
(new function() {
var a = function(x) { document.getElementById('out').innerHTML += x; }
var b = function() { a(d); }
var c = function() { /* ... */ }
var d = "whatever";
// expose two of them ..
this.a = a;
this.b = b;
})['a']('boom.');
来源:https://stackoverflow.com/questions/16722489/javascript-access-function-in-closure