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 ?
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 ?
No, you cannot do it from just the function alone, the function name is not carried along with the function reference.
The name? No, you can't. What you can do however is test the reference. Something like this:
function foo()
{
}
function bar()
{
}
function a(b : Function)
{
if( b == foo )
{
// b is the foo function.
}
else
if( b == bar )
{
// b is the bar function.
}
}
Are you merely looking for a reference so that you may call the function again after? If so, try setting the function to a variable as a reference. var lastFunction:Function;
var func:Function = function test():void
{
trace('func has ran');
}
function A(pFunc):void
{
pFunc();
lastFunction = pFunc;
}
A(func);
Now if you need to reference the last function ran, you can do so merely through calling lastFunction.
I am not sure exactly what you are trying to do, but perhaps this can help.
Brian Hodge
blog.hodgedev.com
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:
I don't know if it helps, but can get a reference to the caller of the function which arguments (as far as I know just in ActionScript 3).
Michael Prescot's answer only begins to answer the question. There are all kinds of different situations in which the stack trace will be different. For example if you are calling it in a constructor the constructor will look different depending if you are using the external player in the IDE or the player in a web page. For a function that gets the function name of its caller and also parses out things like class and package name check out this article
See here a simple example for finding the function name from outside the function
http://flashontherocks.com/2010/03/12/getting-function-name-in-actionsctipt-3/