Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 980
Geuis Avatar asked Dec 20 '09 11:12

Geuis


1 Answers

You declared an anonymous function with

function(){

You should declare it as

function testfunc(){

to get the name printed.

like image 100
Eric Bréchemier Avatar answered Oct 12 '22 10:10

Eric Bréchemier