watagal Posted May 24, 2006 Share Posted May 24, 2006 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 More sharing options...
Jack McKalling Posted May 24, 2006 Share Posted May 24, 2006 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 More sharing options...
watagal Posted May 24, 2006 Author Share Posted May 24, 2006 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 More sharing options...
Jack McKalling Posted May 24, 2006 Share Posted May 24, 2006 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 More sharing options...
adservio Posted May 24, 2006 Share Posted May 24, 2006 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 More sharing options...
watagal Posted May 25, 2006 Author Share Posted May 25, 2006 yes, thankyou - that's the idea. I was incorrectly passing the function as a string (in quotes). Following your example, it works!TA Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now