2

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")');
G-J
  • 1,080
  • 2
  • 16
  • 32
  • I did yet another Google search and found out that I can just do this... `eval(action);` See... http://jsfiddle.net/XxT2B/1/ – G-J Apr 04 '13 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 Apr 04 '13 at 17:49
  • @G-J see this question: http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea or my answer for some reasons why eval is a bad idea. – Ben McCormick Apr 04 '13 at 17:49

6 Answers6

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");
});
James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • Perfect!!! That did the trick. I would vote it up but since I don't have enough reputation "yet", it won't let me. – G-J Apr 04 '13 at 19:15
  • 1
    @G-J Happy to help. Just be sure to accept the best answer (with the green checkmark). As a bonus, I think you also get some more rep from accepting your first answer. I noticed that you have not accepted an answer in any of your previous questions. You should consider re-visiting those questions and accepting the most helpful answer if one exists. – James Montagne Apr 04 '13 at 19:26
2

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
Bucket
  • 7,415
  • 9
  • 35
  • 45
  • Good point on the sanitation. I would vote it up but since I don't have enough reputation "yet", it won't let me. – G-J Apr 04 '13 at 19:17
1

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();
Pointy
  • 405,095
  • 59
  • 585
  • 614
1

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.

Community
  • 1
  • 1
Ben McCormick
  • 25,260
  • 12
  • 52
  • 71
-1

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.

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
  • 1
    You **could**... but you shouldn't. – James Montagne Apr 04 '13 at 17:46
  • Whyever not? `eval` is one truly unique JS method that can get otherwise-difficult-tasks done. The questioner's intentions in passing 'alert("xyz");' do seem exactly pointing at something like eval. – Vinod Vishwanath Apr 04 '13 at 17:48
  • It's actually not unique to JS at all. Here is a non-JS specific explanation: http://stackoverflow.com/questions/2571401/why-exactly-is-eval-evil There are a million others. – James Montagne Apr 04 '13 at 17:51
  • Sorry but that thread doesn't tell me something I don't know. (Not that I disagree with the reasons raised) I do know of scenarios where it's helpful, and I've used them in web-apps without having to regret it. – Vinod Vishwanath Apr 04 '13 at 17:55
-1

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'));
Potato
  • 397
  • 4
  • 16