Javascript:可以通过变量将函数命令传递给另一个函数吗?

Mar*_*ark 1 javascript function

好的,希望这是正确的.我正在构建一个通用的javascript函数,它将构建一个菜单,然后构建每个菜单项将调用的函数.为此,我需要传递要为每个选项调用的命令列表.

例如:

var thecall = 'alert("hi, this works");';

function myfunction(thecall)
{
  //In here I want to excute whatever commands is listed in variable thecall
 .....
}
Run Code Online (Sandbox Code Playgroud)

我敢肯定这样做是完全愚蠢的,但我不知道怎么做.

基本上,我需要我的函数在变量的基础上执行其他功能.

谢谢!!

Gol*_*wby 6

我告诉你如何使用它让我有点发烧友好.

var thecall = function(name){alert("hi " + name + ", this works");};

function myFunction(function_ref)
{
  function_ref('Mark');
}

myFunction(thecall);
Run Code Online (Sandbox Code Playgroud)