1 Reply Latest reply on Dec 13, 2007 6:08 PM by Peter Flynn (Adobe)

    Actionscript - Call function from string (callback)

    iceboxqs Level 1
      Hopefully this is simple.

      I have a function that has a string passed into it. The string is the name of the function I will need to call a little later on.
      If I have a string that is named after a function how do I use it to call that function?

      Example:

      public function someFunction(functionName:String):void
      {
      //do whatever

      //at this point I need to somehow call the function that is held in the string functionName

      functionName() //obviously this doesn't work, what needs to happen here?
      }
        • 1. Re: Actionscript - Call function from string (callback)
          Peter Flynn (Adobe) Adobe Employee
          If you have access to an instance of the class that the function is part of, you can do the following:

          public function someFunction(fnName:String, inst:Object):void
          {
          var fn:Function = inst[fnName];
          fn();
          }

          If you don't want to deal with functions associated with an object, you can just pass around standalone anonymous functions:

          public function someFunction2(fn:Function) {
          fn();
          }
          ...
          someFunction2( function():void {
          trace("Yo");
          } );

          Hope that helps!

          - Peter