![](/img/trans.png)
[英]How to execute a JavaScript function when I have its name as a string
[英]How to execute a private JavaScript function when I have its name as a string
我正在尝试从返回的对象中执行私有函数。 如果我提前知道函数的名称,这很容易,但在这种情况下,我不知道有哪些函数可用。 我所拥有的是一个字符串,其中包含要调用的函数的名称。 有没有办法通过字符串调用这些函数?
function foo() {
// some misc. chunk of valid javascipt code
function bar() {
console.log("hello");
}
// end misc. code
// I would like to avoid doing this if I don't have to
var executableFn = {};
executableFn.test = function() {
bar();
}
// end
return {
// This works but requires I know the name of the funciton ahead of time.
// All I have is a string name of the function to call.
funcRunner0: function() {
bar();
},
// My ideal method for calling but does not work
funcRunner1: function(functionName) {
foo[functionName]();
},
// This works but I'm trying to avoid eval. I'm not sure if this is not so bad
funcRunner2: function(functionName) {
var func = eval(functionName);
func();
},
// This works. I'm not sure if this is worse than funcRunner2 or the same;
funcRunner3: function(functionName) {
eval(functionName + "()");
},
// This works but requires the executableFn object which I would like to avoid if at all possible.
funcRunner4: function(functionName) {
executableFn[functionName]();
},
};
}
var bas = foo();
// This works but assumes I know the name of the function to call which I don't.
bas.funcRunner0();
// This doesn't work
bas.funcRunner1("bar");
// This works
bas.funcRunner2("bar");
// This works
bas.funcRunner3("bar");
// This works but is not my ideal
bas.funcRunner4("test");
这些是我用来调用此功能的所有方法。 您认为用字符串调用bar函数的最佳方法是什么? 谢谢你的帮助。
我理想的呼叫方法但不起作用
foo[functionName]();
是的,那是试图访问foo
函数的[public]属性。 例如,它可以使用"call"
方法。
这有效,但我试图避免评估。 我不确定这不是那么糟糕
var func = eval(functionName); func();
这有效。 我不确定这是否比funcRunner2更差或相同;
eval(functionName + "()");
从eval
的角度来看,两者都和另一个一样糟糕。 选项1确实使得更容易使用参数[不需要动态评估]。
这可以工作,但需要exec对象,如果可能的话我想避免。
exec[functionName]();
这只是做到这一点的方式。 由于exec
是foo
范围内的局部变量,因此您甚至可以拥有自己的私有性。 看来你也已经为publicObj
切换了exec
// I would like to avoid doing this if I don't have to var publicObj = {}; publicObj.test = function() { bar(); }
publicObj
变量尽管名称不公开,但它是用var
关键字声明的本地变量! 顺便说一下,你可以把它简化为
var exec = {
test: bar
};
为什么不创建一个私有命名空间(对象)并使用它而不是exec / eval?
function foo() {
var private_ns = {
bar : function() {
console.log('bar', arguments);
}
};
return {
call_func: function(name) {
if(name in private_ns) {
var args = [].slice.call(arguments, 1)
return private_ns[name].apply(null, args);
} else {
console.error('no such function');
}
}
};
}
var f = foo();
> f.call_func('bar', 1, 2, 3);
bar { '0': 1, '1': 2, '2': 3 }
> f.call_func('noop');
no such function
它有点类似于你的“不理想”解决方案,但有点不同。 如果将所有函数组织到对象中,则可以调用它们。 在调用原始函数的对象中不需要其他函数。
function foo() {
// all functions stored in this object
var functions = {
bar : function() {
console.log("hello");
}
};
return {
funcRunner: function(funcName) {
functions[funcName]();
}
};
}
var bas = foo();
bas.funcRunner("bar");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.