Jump to content

Passing a Function to a function


watagal

Recommended Posts

greetings,I need help in the syntax for passing a function as an argument for another function and then call it:

function wrapper(f){      var return = f;}

I've tried every which way I can think of -- any help?TIA

Link to comment
Share on other sites

Do you mean like this?

function MyFunction (some,somemore){ some code}MyFunction(anotherfunction(),"string")
?Then remember the function anotherfunction() should return a value, else there wouldn't be anything left to pass :)
Link to comment
Share on other sites

No, I don't want to pass the returned value of a function, rather I want to pass a function name and then subsequently execute it within.The idea is to write a wrapper function for several other functions and add additional functionality.Clear as mud?WG

Link to comment
Share on other sites

So you want the function name to pass over to another function :)Is it the case that there are more of those function names then, that you want to be passed variably to this function? If it is a preset number of possible functions, rather that passing its name you can pass an integer.function zero(something,func){if (func == 1) { func_one() }else if (func == 2) { func_two() }...}etcetera :)I can't guarantee that passing a function name will work, if then to be executed as such :)

Link to comment
Share on other sites

I believe it will pass OK as an argument:

  <script type="text/javascript">  function sayHello(who) {  return 'Hello ' + who;  }  function sayGoodbye(who){  return 'Goodbye ' + who;  }  function doThat(func, whom) {  var str = func(whom);  alert(str);  }  doThat(sayHello, 'Bob');  doThat(sayGoodbye, 'Bob');  </script>

Is that what you are looking to do?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...