Jump to content

Is there a way to return a function name?


watagal

Recommended Posts

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

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

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

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

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

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...