views:

1888

answers:

7

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 ?

A: 

No, you cannot do it from just the function alone, the function name is not carried along with the function reference.

CookieOfFortune
+2  A: 

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.
   }
}
Luke
A: 

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

Brian Hodge
+5  A: 

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

Michael Prescott
I would say be careful using this in your system design, it is pretty brittle code, as in, if someone at Adobe decides to rephrase their stack trace message your code breaks. So maybe ask yourself if you really need to know the functions name to solve the problem you have.
Brian Heylin
Agreed. I use it primarily for debugging, and not so much lately since I can step through code with Flex Builder Pro.
Michael Prescott
very cool idea ... but works only in debug player!!!if that is sufficient, ok, but in general this still does not solve the problem ....
back2dos
A: 

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).

Hippo
You can't get the caller, but you do have a reference to the callee.
joshtynjala
A: 

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

Niko Limpika
A: 

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/

Oliver