2

I have a function called getItem. I want to read the name of this function using code from within it. Is this possible?

function getItem(){ 
   var functionName =   //how do I read the function name;
   alert(functionName)  //outputs 'getItem'
}
5
  • 1
    Who says a function even has to have a name? After all, javascript has great support for un-named functions. Commented Apr 8, 2011 at 18:51
  • @Joel Coehoorn, I know what you mean, but this function has a name because I made sure to give it one. Is there a way to get that name from within the function? Commented Apr 8, 2011 at 18:52
  • 1
    What problem are you trying to solve? Are you writing a testing framework and injecting this code into other functions? There might be a way to solve your problem without requiring the name of a function. Commented Apr 8, 2011 at 18:56
  • possible duplicate of Can I get the name of the currently running function in javascript? Commented Apr 8, 2011 at 19:04
  • Possible duplicate of Can I get the name of the currently running function in JavaScript? Commented May 11, 2019 at 22:58

3 Answers 3

3

try this:

function getItem(){ 
   var functionName =   arguments.callee.name;
   alert(functionName)  //outputs 'getItem'
}

here is the fiddle: http://jsfiddle.net/maniator/xGzKA/

also see this previous stack Q for another solution

Sign up to request clarification or add additional context in comments.

7 Comments

arguments.callee is deprecated in strict JS, just as a point of note.
@Shadow Wizard, in IE you have to use the solution in the other stack Q
@Neal you better add this to the answer then. IE8 still has fair share.. :)
@Shadow Wizard its already there. i show the other solutions local :-P
@DDavies maybe you can explain me then this phrase from MDC: "JavaScript 1.4: Deprecated callee as a property of Function.arguments, retained it as a property of a function's local arguments variable." (not trying to be a d**k here, I truly don't understand in which situations is deprecated and in which is not)
|
0

Though you should probably not use this, you can do this for IE, Firefox, Chrome, Safari, and Opera (I admit arguments.callee is convenient despite the valid reservations expressed about it, as would be arguments.callee.name if the Function.name property were standard):

function getFuncName (fn) {
    var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
    return name ? name[1] : '(Anonymous)';
}

Comments

-1

Try this,

function getItem(){
    var functionName =  arguments.callee.name;
    alert(functionName);
}

http://jsfiddle.net/XPGxE/

2 Comments

@Neal Didnt mean to copy. I posted the same answer almost the same time as you. Saw your answer only after posting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.