使用字符串调用JavaScript函数名称?
问题描述:
如何将事件连接到我已定义为字符串的函数名?
How can I hook up an event to a function name I have defined as a string?
我正在使用Prototype.js,尽管这不是Prototype- speficic。
I'm using Prototype.js, although this is not Prototype-speficic.
$(inputId).observe('click', formData.fields[x].onclick);
这会导致JavaScript抱怨我的处理程序不是函数。我不希望我们使用 eval()
。
This would result in JavaScript complaining that my handler is not a function. I would prefer not us use eval()
.
答
如果函数在全局范围内,您可以使用window对象获取它:
If the function is in the global scope, you can get it using the window object:
var myFunc = window[myFuncName];