views:

1274

answers:

6

I'm trying to get the name of the currently running function. From what I've read, this should be possible using:

(arguments.callee.toString()).match(/function\s+(\[^\s\(]+)/)

However, when I run this in Firefox and Safari (latest versions on Mac) the name is not returned.

console.log( arguments.callee ) returns the source of the function, but not the assigned name. arguments.callee.name returns an empty string.

My sample code is as follows:

var testobj = {
    testfunc: function(){
        console.log( (arguments.callee.toString()).match(/function\s+(\[^\s\(]+)/) );
    }
}
testobj.testfunc();
+6  A: 

You declared an anonymous function with

function(){

You should declare it as

function testfunc(){

to get the name printed.

Eric Bréchemier
Be aware that named function expressions (which is what you're suggesting) behave oddly in IE. See http://yura.thinkweb2.com/named-function-expressions/
Tim Down
@Tim.Down good point and interesting read.
Eric Bréchemier
To clarify: in the case of the object style above that would be `{ testfunc: function testfunc() { ...`. This is also nice to do when debugging things in the browser, helps the debugger help you see which function you have problems in.
clacke
+2  A: 
/function\s+(\[^\s\(]+)/

What's with the backslash before [? I don't think you want a literal square bracket here. Without that it should work.

Although I'd strongly recommend against anything to do with sniffing function name or especially sniffing caller function. Almost anything you might do using these hideous hacks will be better done using some combination of closures and lookups.

bobince
A: 

First of all, the function doesn't have a name. The function name is what you put in-between function and the arguments list (...). Here's how to get a function's name (don't use the name property, as it can be changed):

var fName = arguments.callee.toString(0).match(
  /^function\s*(?:\s+([\w\$]*))?\s*\(/
);
fName = (fName ? fName[1] : "");
Eli Grey
+2  A: 

The typical arguments.callee hacks don't work here because what you've done is assign an anonymous function as the value for the object's 'testfunc' key. In this case the hacking even gets worse, but it can be done, as follows:

var testobj = {
    testfunc: function(){
      for (var attr in testobj) {
              if (testobj[attr] == arguments.callee.toString()) {
                  alert(attr);
                  break;
                }
            }
    }
}
testobj.testfunc();
George Jempty
A: 

I found that if you simply log the function object, like so:

console.log(arguments.callee)

or

console.debug(arguments.callee)

that you simply get the function name in the console log with some options.

Dave Van den Eynde
A: 

On firefox 3.5, Safari 5, and Chrome 6.0 you can use:

function myFunctionName() {
 alert("Name is " + arguments.callee.name );
}

myFunctionName();

You can also get the function that called the current one using arguments.callee.caller.

Ben Clayton