views:

834

answers:

3

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.

+3  A: 

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(...);
    }
  }
}
Dan Monego
You should also avoid casting the object you're checking, as casting is very expensive, using something like 'is' or 'as' - as this example does, is much better.
quoo
Correct me if I'm wrong, but I don't think you can cast to Function.
Tyler Egeto
A: 

I've posted a simple example for finding the function name from outside the function

http://flashontherocks.com/2010/03/12/getting-function-name-in-actionsctipt-3/

Oliver
A: 

Now, Oliver, if you're going to post a link, why would you subsequently delete the page it refers to?

Seems kinda silly to do