动作 - 获取当前函数的名称 [英] Actionscript - Obtain the name of the current function

查看:159
本文介绍了动作 - 获取当前函数的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从该函数内部得到一个函数的名称。例如:

I want to get the name of a function from inside that function. e.g.:

function blah() {
    //I want to get the string "blah" here, from the function's name
}

或者至少在功能对象?

Or at least the Function object?

推荐答案

使用的 arguments.callee的得到引用当前函数。

Use arguments.callee to get a reference to the current function.

我要获取函数名,这是一个有点棘手:所有功能都被视为方法闭包(的code片可被传递作为参数),所以他们没有自己的参考一个封闭类的类型,它们也没有一个现名。

I you want to get the function name, it is a bit trickier: All functions are treated as method closures (pieces of code which can be passed around as an argument), so they do not own a reference to an enclosing class type, nor do they have a "current name".

不过,如果(和的只有的IF)的方法是公共的,和你想从一个实例对象的类声明包含的方法来获取方法的名称,你可以使用的不如describeType

However, if (and only if) the method is public, and you want to get the method name from the class declaration of an instance object containing the method, you can use describeType:

public function someFunction() : void {
    var callee:Function = arguments.callee;
    trace (getFunctionName(callee, this)); // ==> someFunction
}

private function someOtherFunction() : void {
    var callee:Function = arguments.callee;
    trace (getFunctionName(callee, this)); // ==> not found
}

private function getFunctionName (callee:Function, parent:Object):String {
    for each ( var m:XML in describeType(parent)..method) {
        if ( parent[m.@name] == callee) return m.@name;
    }
    return "not found";
}

请注意,当你调用这个是行不通的 someFunction()从构造函数,因为该对象未完全实例化 - 不如describeType(本) 在构造函数会导致编译错误。

Note that this would not work when you call someFunction() from a constructor, because the object is not fully instantiated - describeType(this) in a constructor would cause a compilation error.

这篇关于动作 - 获取当前函数的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆