动作脚本函数可以找出自己的名称吗?

Posted

技术标签:

【中文标题】动作脚本函数可以找出自己的名称吗?【英文标题】:can an actionscript function find out its own name? 【发布时间】:2010-10-17 05:16:08 【问题描述】:

鉴于以下情况

function A(b:Function)    

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

【问题讨论】:

【参考方案1】:

我使用以下:

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

【讨论】:

我会说在您的系统设计中使用它时要小心,这是非常脆弱的代码,例如,如果 Adob​​e 的某个人决定重新表述他们的堆栈跟踪消息,您的代码就会中断。所以也许问问自己,你是否真的需要知道函数名称来解决你遇到的问题。 同意。我主要将它用于调试,最近不那么频繁了,因为我可以使用 Flex Builder Pro 单步调试代码。 非常酷的想法...但仅适用于调试播放器!!!如果这足够了,好的,但总的来说这仍然不能解决问题.... 借助 Air 3.5 和 Flash 11.5,现在可以获得发布版本的堆栈跟踪。我同意这种方法是有风险的 - 堆栈跟踪字符串格式的微小变化很容易破坏代码,但它有效,并且运作良好。【参考方案2】:

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

【讨论】:

不错的方法,但getSavedThis() 仅适用于 Flash 播放器的调试版本。 谢谢你,如果其他人找不到包:import flash.utils.describeType;导入 flash.sampler.getSavedThis;【参考方案3】:

这是一个简单的实现

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

在一个名为 FunctionUtils 的文件中,我把这个...

    /** 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;
    

【讨论】:

【参考方案4】:

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

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.
   

【讨论】:

【参考方案5】:

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

【讨论】:

你无法获取调用者,但你确实有对被调用者的引用。【参考方案6】:

通常将 arguments.callee 与 toString()match 一起使用:

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

参考文献

arguments callee method

Tracing Function Arguments

【讨论】:

【参考方案7】:

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

var func:Function = function test():void

    trace('func has ran');


function A(pFunc):void

    pFunc();
    lastFunction = pFunc;

A(func);

现在如果你需要引用上次运行的函数,你可以通过调用 lastFunction 来实现。

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

【讨论】:

以上是关于动作脚本函数可以找出自己的名称吗?的主要内容,如果未能解决你的问题,请参考以下文章

如何从大fasta文件中找出自己想要的序列

我现在想把自己写的python模块源代码封装成dll,然后在别的python脚本里调用,可以吗?

Windows 批处理文件可以确定自己的窗口名称吗?

机器人可以像用户一样响应命令吗?

Java 的方法可以“知道”它自己的名字吗? [复制]

ps批量处理图片分辨率 rename批量修改名称