简体   繁体   中英

How do I pass an action to a javascript function

I could explain my problem but it is likely easier to demonstrate it...

If you take a look at http://jsfiddle.net/XxT2B/ you'll see my issue. I am having trouble figuring out how to pass an action to a function. You'll see what I mean.

Please note that the action could be different based on what calls the function. The action may be an alert on time and something different the next.

Here is my code...

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")');

You can pass a function as a parameter to another function.

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");
});

You can pass a function into abc() , but be sure to sanitize

function abc(action)
{
    alert('This function is named "abc"');

    if(typeof(action) == "function") { //sanitize
        action();
    }
}

abc('alert("I like pizza")'); //will execute without a problem
abc(50); //will not run, since 50 is not a function

You just need to instantiate a function:

abc(function() { alert("I like pizza"); });

edit and then to call it, you use the value of your parameter exactly as if it were a function name (because, well it is!):

  action();

The good way:

Pass it as a function:

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")});

the bad way (if your actions need to be strings):

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

    eval(action);
}

abc('alert("I like pizza")');

The second way is not advised because eval causes issues. It can run arbitrary code that can cause unexpected side effects, prevents compiler optimizations, and leads to difficulty debugging (since it can literally do anything depending on what you pass it). More on why eval is bad here.

But it will run an arbitrary string as javascript code like you were asking.

You can use the eval method:

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

    eval(action);
}

abc('alert("I like pizza")');

And that's that.

Don't know what JavaScript version supports this syntax but you also can try:

function abc(action) {
if (typeof(action) != 'function')
            return;
    action();

}

abc(() => console.log('A B C'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM