动态功能不起作用

时间:2014-09-26 08:24:29

标签: javascript jquery dynamic

你好动态函数不起作用 问题在哪里?

DynamicCreateFunction函数有效,但runtimeFunction无效。

http://i.hizliresim.com/VyXkRv.png

  function DynamicCreateFunction(func) {

    var runtimeFunction = new Function(func);
    runtimeFunction ();
}

     function ExpensesGrid()
    {
         alert('working');
    }

2 个答案:

答案 0 :(得分:0)

您正在尝试传递function作为参数,您可以这样做:

function DynamicCreateFunction(callback){
   callback.call(this); //or .apply() or simply callback()
}

如果您想执行ExpensesGrid,请执行

DynamicCreateFunction(ExpensesGrid);

文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

答案 1 :(得分:0)

 hello thanks for the reply but I see the `"call" is not supported error` why ?

    function DynamicCreateFunction(callback){
   callback.call(this); //or .apply() or simply callback()
}

但问题解决了,替代解决了,“功能正在发挥作用”

dynamicFunction = window[func]; 
dynamicFunction(); 

谢谢 -