Javascript function knows its name

I have a function called getItem. I want to read the name of this function using the code from it. Is it possible?

function getItem(){ var functionName = //how do I read the function name; alert(functionName) //outputs 'getItem' } 
+2
source share
3 answers

try the following:

 function getItem(){ var functionName = arguments.callee.name; alert(functionName) //outputs 'getItem' } 

here is the fiddle: http://jsfiddle.net/maniator/xGzKA/

also see this previous Q stack for another solution

+3
source

Although you probably shouldn't use this, you can do it for IE, Firefox, Chrome, Safari, and Opera (I admit that arguments.callee convenient, despite the fact that it expresses certain reservations, like arguments.callee.name , if Function.name were standard):

 function getFuncName (fn) { var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn); return name ? name[1] : '(Anonymous)'; } 
0
source

Try it,

 function getItem(){ var functionName = arguments.callee.name; alert(functionName); } 

http://jsfiddle.net/XPGxE/

-1
source

Source: https://habr.com/ru/post/909228/


All Articles