Call a function without using eval?

I have an object attached to both dataa DIV. I also have a string that contains the function name of this object.

function callFunction(divId, funcName, data) {
  o = $(divId).data('myObject');
  // how do I call o.funcName(data) ???

// Somewhere else...
callFunction('#myDivId', "myFunction", someData);

divIdand funcNameactually come from the Java applet on the page, so they are strings.

+3
source share
2 answers

Assuming this is a global function:

window['foo']()

coincides with

foo()
+4
source

Depends on the area of ​​your functions. It is attached to the window, you can do it;

<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>
+1
source

All Articles