can an actionscript function find out its own name?

Rotem Hadary picture Rotem Hadary · Apr 2, 2009 · Viewed 10.3k times · Source

given the following

function A(b:Function)   { }

If function A(), can we determine the name of the function being passed in as parameter 'b' ? Does the answer differ for AS2 and AS3 ?

Answer

user56512 picture user56512 · Apr 3, 2009

I use the following:

private function getFunctionName(e:Error):String {
    var stackTrace:String = e.getStackTrace();     // entire stack trace
    var startIndex:int = stackTrace.indexOf("at ");// start of first line
    var endIndex:int = stackTrace.indexOf("()");   // end of function name
    return stackTrace.substring(startIndex + 3, endIndex);
}

Usage:

private function on_applicationComplete(event:FlexEvent):void {
    trace(getFunctionName(new Error());
}

Output: FlexAppName/on_applicationComplete()

More information about the technique can be found at Alex's site:

http://blogs.adobe.com/aharui/2007/10/debugging_tricks.html