ActionScript 3的自省 - 函数名 [英] Actionscript 3 introspection -- function names
问题描述
我想遍历每个对象的成员。对于每一个成员,我检查,看它是否是一个函数或没有。如果它是一个功能,我想获得它的名字,并执行基于函数的名称一些逻辑。我不知道这甚至有可能,但。是吗?任何提示?
I am trying to iterate through each of the members of an object. For each member, I check to see if it is a function or not. If it is a function, I want to get the name of it and perform some logic based on the name of the function. I don't know if this is even possible though. Is it? Any tips?
例如:
var mems: Object = getMemberNames(obj, true);
for each(mem: Object in members) {
if(!(mem is Function))
continue;
var func: Function = Function(mem);
//I want something like this:
if(func.getName().startsWith("xxxx")) {
func.call(...);
}
}
我有一个很难找到多少就做这个。感谢您的帮助。
I'm having a hard time finding much on doing this. Thanks for the help.
推荐答案
您伪code是接近做你想要什么。而不是使用getMemberNames,但是,它可以得到私有方法,你可以遍历一个简单的for..in循环的成员,并获得使用括号中的成员的值。例如:
Your pseudocode is close to doing what you want. Instead of using getMemberNames, however, which can get private methods, you can loop over the members with a simple for..in loop, and get the values of the members using brackets. For example:
public function callxxxxMethods(o:Object):void
{
for(var name:String in o)
{
if(!(o[name] is Function))
continue;
if(name.startsWith("xxxx"))
{
o[name].call(...);
}
}
}
这篇关于ActionScript 3的自省 - 函数名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!