Geuis
Geuis

Reputation: 42277

javascript - arguments.callee.toString() and arguments.callee.name does not return function name

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();

Upvotes: 8

Views: 15828

Answers (8)

yckart
yckart

Reputation: 33408

Function.prototype.getName = function(fn) {
    if(Function.name || Function.prototype.name) return this.name;
    return this.toString().match(/^function\s+(\w+)\s*\(/)[1];
};

Upvotes: 2

Dexygen
Dexygen

Reputation: 12561

The typical arguments.callee hacks don't work here because what you've done is assigned 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();

Upvotes: 8

Murray Todd Williams
Murray Todd Williams

Reputation: 331

I think there's a much cleaner and elegant solution to all this. Assuming the function is a member of some higher-level object—and that's always going to be the case, even if the function's owner is "window" or some other global object, we can access the global object via the this keyword, we can access the function itself via arguments.callee and we can access all the parent's object (function) names via for (var o in this), so you should be able to get the desired information fairly easily as...

returnMyName = function() {
  for (var o in this) {
    if (arguments.callee===this[o]) return o;
  }
};

That should be robust and avoid any weird IE browser behaviors accessing named functions, etc.

Upvotes: 2

Ben Clayton
Ben Clayton

Reputation: 82219

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.

Upvotes: 3

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17415

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.

Upvotes: 0

Eli Grey
Eli Grey

Reputation: 35895

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] : "");

Upvotes: 0

bobince
bobince

Reputation: 536469

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

Upvotes: 2

user207968
user207968

Reputation:

You declared an anonymous function with

function(){

You should declare it as

function testfunc(){

to get the name printed.

Upvotes: 11

Related Questions