Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  200] [ 7]  / answers: 1 / hits: 173631  / 15 Years ago, thu, june 18, 2009, 12:00:00

Is it possible to do this:



myfile.js:
function foo() {
alert(<my-function-name>);
// pops-up foo
// or even better: myfile.js : foo
}


I've got the Dojo and jQuery frameworks in my stack, so if either of those make it easier, they're available.


More From » jquery

 Answers
17

In ES5 and above, there is no access to that information.



In older versions of JS you can get it by using arguments.callee.



You may have to parse out the name though, as it will probably include some extra junk. Though, in some implementations you can simply get the name using arguments.callee.name.



Parsing:



function DisplayMyName() 
{
var myName = arguments.callee.toString();
myName = myName.substr('function '.length);
myName = myName.substr(0, myName.indexOf('('));

alert(myName);
}



Source: Javascript - get current function name.



[#99285] Monday, June 15, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
manuel

Total Points: 747
Total Questions: 96
Total Answers: 95

Location: Argentina
Member since Thu, Mar 18, 2021
3 Years ago
;