I have a function called getItem. I want to read the name of this function using code from within it. Is this possible?
function getItem(){
var functionName = //how do I read the function name;
alert(functionName) //outputs 'getItem'
}
I have a function called getItem. I want to read the name of this function using code from within it. Is this possible?
function getItem(){
var functionName = //how do I read the function name;
alert(functionName) //outputs 'getItem'
}
try this:
function getItem(){
var functionName = arguments.callee.name;
alert(functionName) //outputs 'getItem'
}
here is the fiddle: http://jsfiddle.net/maniator/xGzKA/
also see this previous stack Q for another solution
Though you should probably not use this, you can do this for IE, Firefox, Chrome, Safari, and Opera (I admit arguments.callee
is convenient despite the valid reservations expressed about it, as would be arguments.callee.name
if the Function.name property were standard):
function getFuncName (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
return name ? name[1] : '(Anonymous)';
}
Try this,
function getItem(){
var functionName = arguments.callee.name;
alert(functionName);
}