watagal Posted May 25, 2006 Share Posted May 25, 2006 greetings,along the same thread of passing a function as an argument in another function, is there a way to get the name of the passed function as a string. Like: function wrapper(func){ alert(func.name.toString);} If the passed function was sayHello(), than the alert should display "sayHello".TIAWG Link to comment Share on other sites More sharing options...
scott100 Posted May 25, 2006 Share Posted May 25, 2006 does this help, split the string before the ( <html><body><script type="text/javascript">str="sayHello()";chop=str.split("(");document.write(chop[0]);</script></body></html> Link to comment Share on other sites More sharing options...
watagal Posted May 25, 2006 Author Share Posted May 25, 2006 I don't think so. When I pass a function as an argument of another function, it is not a string, rather a reference to some code to execute. What I want is to get the actual name of the passed function.Thanks,WG Link to comment Share on other sites More sharing options...
adservio Posted May 26, 2006 Share Posted May 26, 2006 You and Scott have the right ingredients we just need to mix them differently. function doThat(func, whom) { var str = func(whom); var temp = func.toString(); temp = temp.substring(9, temp.indexOf("(")); alert(str + ' was a result of the function '+ temp); } The question is.. would it be easier to just pass another argument as a string representing the function?Glad the other post worked out for you. Link to comment Share on other sites More sharing options...
watagal Posted May 26, 2006 Author Share Posted May 26, 2006 Thanks, that's the ticket! I wanted to do it this way (in lieu of passing the actual function name as a string) is that it is less maintenance down the road.Thanks so much! 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