最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I pass an action to a javascript function - Stack Overflow

programmeradmin1浏览0评论

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

If you take a look at / 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")');

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

If you take a look at http://jsfiddle/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")');
Share Improve this question asked Apr 4, 2013 at 17:42 G-JG-J 1,0682 gold badges18 silver badges32 bronze badges 3
  • I did yet another Google search and found out that I can just do this... eval(action); See... jsfiddle/XxT2B/1 – G-J Commented Apr 4, 2013 at 17:46
  • @GJ Technically you can, but you shouldn't. Google "eval is evil" and read one of many articles on why you should avoid eval. – James Montagne Commented Apr 4, 2013 at 17:49
  • @G-J see this question: stackoverflow./questions/86513/… or my answer for some reasons why eval is a bad idea. – Ben McCormick Commented Apr 4, 2013 at 17:49
Add a ment  | 

6 Answers 6

Reset to default 5

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

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 piler 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 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();

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'));
发布评论

评论列表(0)

  1. 暂无评论