2009-04-02 8 views

risposta

2

Il nome? No, non puoi. Quello che puoi fare è comunque testare il riferimento. Qualcosa di simile a questo:

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. 
    } 
} 
0

Stai solo alla ricerca di un punto di riferimento in modo che si può chiamare la funzione di nuovo dopo? Se è così, prova a impostare la funzione su una variabile come riferimento. var lastFunction: Function;

var func:Function = function test():void 
{ 
    trace('func has ran'); 
} 

function A(pFunc):void 
{ 
    pFunc(); 
    lastFunction = pFunc; 
} 
A(func); 

Ora, se è necessario fare riferimento l'ultima funzione corse, è possibile farlo solo attraverso chiamando lastFunction.

Non sono sicuro di cosa stiate cercando di fare, ma forse questo può essere d'aiuto.

15

Io uso il seguente:

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()); 
} 

uscita: FlexAppName/on_applicationComplete()

Maggiori informazioni sulla tecnica si possono trovare presso il sito di Alex:

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

+4

Direi fare attenzione utilizzando questo nella vostra progettazione del sistema, è un codice piuttosto fragile, in quanto, se qualcuno in Adobe decide di riformulare il messaggio di tracciamento dello stack, il codice si interrompe. Quindi forse chiediti se hai davvero bisogno di conoscere il nome delle funzioni per risolvere il problema che hai. –

+0

concordato. Lo uso principalmente per il debug, e non tanto ultimamente poiché posso passare al codice con Flex Builder Pro. –

+0

idea molto interessante ... ma funziona solo con il debug player !!! se questo è sufficiente, ok, ma in generale questo ancora non risolve il problema .... – back2dos

0

Non so se è d'aiuto, ma posso ottenere un riferimento al chiamante della funzione che arguments (per quanto ne so solo in ActionScript 3).

+0

Non è possibile ottenere il chiamante, ma si ha un riferimento al destinatario. – joshtynjala

3

Ecco una semplice implementazione

public function testFunction():void { 
     trace("this function name is " + FunctionUtils.getName()); //will output testFunction 
    } 

E in un file chiamato FunctionUtils ho messo questo ...

/** Gets the name of the function which is calling */ 
    public static function getName():String { 
     var error:Error = new Error(); 
     var stackTrace:String = error.getStackTrace();  // entire stack trace 
     var startIndex:int = stackTrace.indexOf("at ", stackTrace.indexOf("at ") + 1); //start of second line 
     var endIndex:int = stackTrace.indexOf("()", startIndex); // end of function name 

     var lastLine:String = stackTrace.substring(startIndex + 3, endIndex); 
     var functionSeperatorIndex:int = lastLine.indexOf('/'); 

     var functionName:String = lastLine.substring(functionSeperatorIndex + 1, lastLine.length); 

     return functionName; 
    } 
6

Ho provato le soluzioni proposte, ma mi sono imbattuto in problemi con tutti loro in determinati punti. Principalmente a causa delle limitazioni per i membri dinamici o. Ho lavorato un po 'e combinato entrambi gli approcci. Intendiamoci, funziona solo per i membri visibili pubblicamente - in tutti gli altri casi viene restituito un valore nullo.

/** 
    * Returns the name of a function. The function must be <b>publicly</b> visible, 
    * otherwise nothing will be found and <code>null</code> returned.</br>Namespaces like 
    * <code>internal</code>, <code>protected</code>, <code>private</code>, etc. cannot 
    * be accessed by this method. 
    * 
    * @param f The function you want to get the name of. 
    * 
    * @return The name of the function or <code>null</code> if no match was found.</br> 
    *   In that case it is likely that the function is declared 
    *   in the <code>private</code> namespace. 
    **/ 
    public static function getFunctionName(f:Function):String 
    { 
     // get the object that contains the function (this of f) 
     var t:Object = getSavedThis(f); 

     // get all methods contained 
     var methods:XMLList = describeType(t)[email protected]; 

     for each (var m:String in methods) 
     { 
      // return the method name if the thisObject of f (t) 
      // has a property by that name 
      // that is not null (null = doesn't exist) and 
      // is strictly equal to the function we search the name of 
      if (t.hasOwnProperty(m) && t[m] != null && t[m] === f) return m;    
     } 
     // if we arrive here, we haven't found anything... 
     // maybe the function is declared in the private namespace? 
     return null;           
    } 
+0

Buon approccio, ma 'getSavedThis()' funziona solo nelle versioni di debug di Flash Player. – Art

+0

Grazie per questo, e nel caso in cui qualcun altro abbia problemi a trovare i pacchetti: import flash.utils.describeType; import flash.sampler.getSavedThis; – Hudson

Problemi correlati