How to find function name from inside of itself
function FunctionA(){
var fName = arguments.callee.toString().match(/function ([^\(]+)/)[1]
alert('Hi, I\'m in a function named '+fName)
}
function FunctionB(){
alert(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}
function FunctionC(){
FunctionA();
FunctionB();
}
FunctionC();
Example of using it in a try-catch block
try {
// Do something here
}
catch(err) {
LogError("Error: " + err.message + ", function " + arguments.callee.toString().match(/function ([^\(]+)/)[1] + ", line: " + err.line);
if (debugMode) $.writeln(err.message + ", function " + arguments.callee.toString().match(/function ([^\(]+)/)[1] + ", line: " + err.line);
}
Get the name of the calling function
if (debugMode) $.writeln("Calling from '" + CallerName() + "'";
function CallerName() {
var arr = $.stack.split(/[\n]/),
callerName = arr[arr.length - 3].replace(/\(.*\)/, "")
return callerName;
}