Actionscript - 获取当前函数的名称

发布于 2024-10-13 04:31:45 字数 169 浏览 4 评论 0 原文

我想从该函数内部获取该函数的名称。例如:

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

或者至少是 Function 对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜心 2024-10-20 04:31:45

使用 arguments.callee 获取对当前函数的引用。

如果你想获取函数名称,这有点棘手:所有函数都被视为方法闭包(可以作为参数传递的代码片段),因此它们不拥有对封闭类类型的引用,也不拥有对封闭类类型的引用。他们有“当前名称”吗?

但是,如果(并且)该方法是公共的,并且您希望从包含该方法的实例对象的类声明中获取方法名称,则可以使用 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(this) 会导致编译错误。

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

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

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

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.

沧笙踏歌 2024-10-20 04:31:45

我只是使用堆栈跟踪来完成此操作,这是 Flash Player 调试器在抛出未处理的错误时显示的内容。这是我的实现:

function blah()
{
    var e:Error = new Error();
    var s:String = e.getStackTrace();
    var functionName:String = s.slice(s.indexOf('/')+1, s.indexOf('('));
    trace(functionName); //blah
}

如果您想将其作为一个单独的函数,您可能需要对字符串进行更复杂的解析。但函数名肯定在堆栈跟踪中。

编辑:重要警告

AS3 Lang Ref 表示 getStackTrace 方法仅适用于 Flash Player/AIR 的 Debugger 版本,否则将返回 null。所以这绝对不是一个可部署的解决方案。对不起。

I just did it with a stack trace, which is what the Flash Player debugger shows when an unhandled error is thrown. Here's my implementation:

function blah()
{
    var e:Error = new Error();
    var s:String = e.getStackTrace();
    var functionName:String = s.slice(s.indexOf('/')+1, s.indexOf('('));
    trace(functionName); //blah
}

You'll probably need to make the parsing of the string a bit fancier, if you want to make this a separate function. But the function name is definitely in the stack trace.

Edit: Important Caveat

The AS3 Lang Ref says that the getStackTrace method only works in the Debugger versions of Flash Player/AIR, and will return null otherwise. So this is definitely not a deployable solution. Sorry.

北斗星光 2024-10-20 04:31:45

您可能无法获取函数的名称,因为它们实际上没有“名称”。就像你的变量没有名称一样。它们只是指向对象的指针。

获取名称没有意义,部分原因是以下示例:

function foo():void 
{
    //get function name.
}
var bar:Function = foo;
bar();

函数就像任何其他对象/变量一样。这个函数会找到什么“名字”? “富”?还是“酒吧”? foo 和 bar 都引用 Function 类型的相同变量。

处理此问题的更好方法是将某些内容作为参数传递给函数:

function foo(myName:String):void 
{
    trace(myName);
}

但是,您可以使用 arguments.callee :)

You probably wont be able to get the name of a function, because they don't really have a "name" as such. Just like your variables don't have names. They are just pointers to an object.

Getting the name doesn't make sense, partly due to the following example:

function foo():void 
{
    //get function name.
}
var bar:Function = foo;
bar();

A function is just like any other object/variable. What "name" would this function find? "foo"? or "bar"? Both foo and bar are referencing the same variable of type Function.

A better way to handle this would be to pass something in to the function as an argument:

function foo(myName:String):void 
{
    trace(myName);
}

You can however, get a reference to the current function using the arguments.callee :)

慈悲佛祖 2024-10-20 04:31:45

为了在 Actionscript 2 中实现这一点,我最近不得不使用一些遗留代码来完成

function getFunctionName(func:Function):String
{
  for(var prop in this)
  {
    if(this[prop] == func)
    {
      return prop;
      break;
    }
  }
}

并使用它,如下所示

trace(getFunctionName(arguments.callee));

To pull this off in Actionscript 2, which I had to do recently with some legacy code

function getFunctionName(func:Function):String
{
  for(var prop in this)
  {
    if(this[prop] == func)
    {
      return prop;
      break;
    }
  }
}

And use it something like this

trace(getFunctionName(arguments.callee));
念﹏祤嫣 2024-10-20 04:31:45

当我需要用于事件系统实现的函数 ID 时,我没有找到一种方法来访问作为参数传递的非匿名函数的名称,因此我不得不编写一个包装器,该包装器不存储任何内容,除了Function 及其 ID

when i needed IDs for functions for my event system implementation i didn't find a way to access a name of a non-anonymous function passed as an argument so i had to write a wrapper that stored nothing except the Function and its ID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文