[英]Call a function without using eval?
I have an object attached as data
to a DIV. 我有一个对象作为
data
附加到DIV。 I also have a string that contains the name of a function of that object. 我还有一个字符串,其中包含该对象的函数名称。
function callFunction(divId, funcName, data) {
o = $(divId).data('myObject');
// how do I call o.funcName(data) ???
// Somewhere else...
callFunction('#myDivId', "myFunction", someData);
The divId
and funcName
are actually coming from a Java applet on the page, which is why they're strings. divId
和funcName
实际上来自页面上的Java applet,这就是为什么它们是字符串。
Assuming it is a global function: 假设它是一个全局函数:
window['foo']()
is the same as to 是一样的
foo()
Depends on your functions scope. 取决于您的功能范围。 It it's attached to the window, you can do this;
它附在窗户上,你可以做到这一点;
<script>
var my_dynamic_function = function(param) {
alert(param);
};
var callFunction = function(divId, funcName, data) {
...
window[funcName]('PARAM!');
};
...
callFunction('#myDivId', 'my_dynamic_function', data);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.