12

给出以下

function A(b:Function)   { }

如果是函数 A(),我们能否确定作为参数 'b' 传入的函数的名称?AS2 和 AS3 的答案是否不同?

4

7 回答 7

15

我使用以下内容:

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

用法:

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

输出:FlexAppName/on_applicationComplete()

有关该技术的更多信息,请访问 Alex 的网站:

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

于 2009-04-03T14:14:04.720 回答
6

我一直在尝试建议的解决方案,但在某些时候我遇到了所有问题。主要是因为对固定动态成员的限制。我做了一些工作并将这两种方法结合起来。请注意,它仅适用于公开可见的成员 - 在所有其他情况下返回 null。

    /**
     * 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)..method.@name;

        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;                                        
    }
于 2011-07-20T16:04:22.380 回答
3

Heres a simple implementation

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

And in a file called FunctionUtils I put this...

    /** 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;
    }
于 2011-07-12T23:51:09.780 回答
2

名字?不,你不能。但是,您可以做的是测试参考。像这样的东西:

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.
   }
}
于 2009-04-02T22:10:49.700 回答
0

我不知道它是否有帮助,但可以获得对函数调用者的引用据我所知,仅在 ActionScript 3 中)。

于 2009-04-06T11:09:53.170 回答
0

您是否只是在寻找参考,以便之后可以再次调用该函数?如果是这样,请尝试将函数设置为变量作为参考。var lastFunction:函数;

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

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

现在,如果您需要引用最后运行的函数,只需调用 lastFunction 即可。

我不确定您到底要做什么,但这也许会有所帮助。

于 2009-04-03T01:09:54.683 回答
0

使用 arguments.calleetoString()match一般:

function foo(){return arguments.callee.toString().match(/\s\w+/).toString()}

参考

于 2014-09-11T19:51:32.013 回答