简体   繁体   English

不使用eval调用函数?

[英]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. divIdfuncName实际上来自页面上的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.

相关问题 如何在不使用Eval的情况下调用匿名函数? - How can I call anonymous function without using Eval? Javascript:在没有eval的情况下调用变量中定义的函数 - Javascript: call a function defined in a variable without eval 使用字符串调用不带eval()的函数 - Use string to call function without eval() 如何在运行时从Javascript动态调用Actionscript 3中的函数而不使用eval()? - How do you dynamically call a function in Actionscript 3 from Javascript at runtime without using eval()? JavaScript,在不使用eval的情况下将私有函数作为公共方法内的字符串调用(Revealing pattern) - JavaScript, call private function as a string inside public method without using eval (Revealing pattern) 如何不使用eval调用存储在字符串中的胖箭头函数? - How do you call fat arrow function stored in string without using eval? 如何在不使用eval()的情况下将完整功能字符串转换为javascript函数 - how to convert a full function string to a javascript function without using eval() 替代使用eval调用略有不同的函数名称? - Alternative to using eval to call slightly different function names? 避免使用 eval() 仅使用动态字符串按名称调用函数 - Avoid using eval() to call function by name with only dynamic string Javascript调用函数作为字符串,多个参数不使用eval() - Javascript call function as string, with multiple parameters not using eval()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM