Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  129] [ 2]  / answers: 1 / hits: 15613  / 15 Years ago, sun, december 20, 2009, 12:00:00

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(/functions+([^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(/functions+([^s(]+)/) );
}
}
testobj.testfunc();

More From » function

 Answers
12

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

[#98029] Wednesday, December 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leifw

Total Points: 88
Total Questions: 103
Total Answers: 103

Location: France
Member since Thu, May 6, 2021
3 Years ago
leifw questions
;