Javascript - 函数名中的变量,可能吗? [英] Javascript - Variable in function name, possible?
本文介绍了Javascript - 函数名中的变量,可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望这个问题不是太简单,但我不知道:(
i hope this question is not too simple, but i have no idea :(
我如何用函数名中的var启动一个函数?
How can i start a function with a var in the function name?
例如......
我的功能
function at_26();
function at_21();
function at_99();
启动功能
var test_id = 21;
at_'+test_id+'(); // doesn't work
我希望有人可以帮助我。
I hope somebody can help me.
提前致谢!
Peter
Thanks in advance! Peter
推荐答案
将您的函数存储在对象中,而不是将它们置于顶级。
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['at_' + test_id]();
...并且避免将它们存储在对象中,但这意味着要在全局范围内播放要避免。
… and avoid having to store them in an object, but this means playing in the global scope which should be avoided.
这篇关于Javascript - 函数名中的变量,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文