Search code examples
apache-flexactionscript-3functionintrospection

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?

example:

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.


Solution

  • 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(...);
        }
      }
    }