Javascript - 函数名中的变量,可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3733580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - Variable in function name, possible?
提问by Peter
i hope this question is not too simple, but i have no idea :(
我希望这个问题不是太简单,但我不知道:(
How can i start a function with a var in the function name?
如何在函数名称中使用 var 启动函数?
For example ...
例如 ...
my functions
我的职能
function at_26();
function at_21();
function at_99();
start the function
启动功能
var test_id = 21;
at_'+test_id+'(); // doesn't work
I hope somebody can help me.
我希望有人可以帮助我。
Thanks in advance! Peter
提前致谢!彼得
回答by Quentin
Store your functions in an object instead of making them top level.
将您的函数存储在一个对象中,而不是将它们置于顶层。
var at = {
at_26: function() { },
at_21: function() { },
at_99: function() { }
};
Then you can access them like any other object:
然后您可以像访问任何其他对象一样访问它们:
at['at_' + test_id]();
You could also access them directly from the window
object…
你也可以直接从window
对象访问它们......
window['at_' + test_id]();
… and avoid having to store them in an object, but this means playing in the global scope which should be avoided.
...并避免将它们存储在一个对象中,但这意味着应该避免在全局范围内播放。
回答by JeanHuguesRobert
You were close.
你很接近。
var test_id = 21
this['at_'+test_id]()
However, what you may want:
但是,您可能想要的是:
at = []
at[21] = function(){ xxx for 21 xxx }
at[test_id]()
回答by Nik
You can also try
你也可以试试
function at_26(){};
function at_21(){};
function at_99(){};
var test_id = 21;
eval('at_'+test_id+'()');
But use this code if you have very strong reasons for using eval. Using eval in javascript is not a good practice due to its disadvantages such as "using it improperly can open your script to injection attacks."
但是,如果您有非常充分的理由使用 eval,请使用此代码。在 javascript 中使用 eval 不是一个好的做法,因为它有一些缺点,例如“使用不当可能会打开你的脚本进行注入攻击”。
回答by bigdave
There is a better way then the window object - which is NOT friendly in firefox - use "self" instead - so in the example posted by Quentin it looks like this:
有一个更好的方法然后 window 对象 - 这在 firefox 中不友好 - 使用“self”代替 - 所以在 Quentin 发布的示例中,它看起来像这样:
self['at_' + test_id]();
self['at_' + test_id]();
回答by NVRM
An example to pass an array of params to those composed functions, .
将参数数组传递给这些组合函数的示例,.
/* Store function names and match params */
let at = {
at_26 : (a,b,c) => at_26(a,b,c),
at_21 : (a,b,c) => at_21(a,b,c),
at_99 : (a,b,c) => at_99(a,b,c),
at_om : (a,b,c,d,e) => at_om(a,b,c,d,e)
}
/* Dynamic fuction name + array of Params */
function dynFunc(name, arrayParams){
return at[name](...arrayParams)
}
/* Usage examples */
dynFunc(`at_`, ["track001", 32, true])
dynFunc("at_" + "om", ["track007", [50, false], 7.123, false, "Bye"])
/* Tests */
function at_99(a,b,c){
console.log("Hi! " + a,b,c)
console.log(typeof(a), typeof(b), typeof(c))
}
function at_om(a,b,c,d,e){
console.log("Hi! " + a,b,c,d,e)
console.log(typeof(a), typeof(b), typeof(c), typeof(d), typeof(e))
}