Actionscript - 获取当前函数的名称
我想从该函数内部获取该函数的名称。例如:
function blah() {
//I want to get the string "blah" here, from the function's name
}
或者至少是 Function 对象?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 arguments.callee 获取对当前函数的引用。
如果你想获取函数名称,这有点棘手:所有函数都被视为方法闭包(可以作为参数传递的代码片段),因此它们不拥有对封闭类类型的引用,也不拥有对封闭类类型的引用。他们有“当前名称”吗?
但是,如果(并且仅)该方法是公共的,并且您希望从包含该方法的实例对象的类声明中获取方法名称,则可以使用 describeType:
请注意,当您从构造函数调用
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:
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.我只是使用堆栈跟踪来完成此操作,这是 Flash Player 调试器在抛出未处理的错误时显示的内容。这是我的实现:
如果您想将其作为一个单独的函数,您可能需要对字符串进行更复杂的解析。但函数名肯定在堆栈跟踪中。
编辑:重要警告
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:
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.您可能无法获取函数的名称,因为它们实际上没有“名称”。就像你的变量没有名称一样。它们只是指向对象的指针。
获取名称没有意义,部分原因是以下示例:
函数就像任何其他对象/变量一样。这个函数会找到什么“名字”? “富”?还是“酒吧”? foo 和 bar 都引用 Function 类型的相同变量。
处理此问题的更好方法是将某些内容作为参数传递给函数:
但是,您可以使用 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:
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:
You can however, get a reference to the current function using the arguments.callee :)
为了在 Actionscript 2 中实现这一点,我最近不得不使用一些遗留代码来完成
并使用它,如下所示
To pull this off in Actionscript 2, which I had to do recently with some legacy code
And use it something like this
当我需要用于事件系统实现的函数 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 theFunction
and itsID