如何将动作传递给 javascript 函数

G-J*_*G-J 2 javascript

我可以解释我的问题,但证明它可能更容易......

如果您查看http://jsfiddle.net/XxT2B/,您会看到我的问题。我无法弄清楚如何将操作传递给函数。你会明白我的意思。

请注意,根据调用函数的内容,操作可能会有所不同。该动作可能是按时发出警报,然后是不同的内容。

这是我的代码...

function abc(action)
{
    //Do a bunch of stuff first and then do the action sent to this function
    alert('This function is named "abc"');

    //This is the part I do not know how to do.
    //The action might be an alert or something totally different so I can't just pass text
    //I need to know how to execute the action passed.
    action;
}

abc('alert("I like pizza")');
Run Code Online (Sandbox Code Playgroud)

Jam*_*gne 5

您可以将一个函数作为参数传递给另一个函数。

function abc(action)
{
    //Do a bunch of stuff first and then do the action sent to this function
    alert('This function is named "abc"');

    action();
}

abc(function(){
    alert("I like pizza");
});
Run Code Online (Sandbox Code Playgroud)