Loading ...
Sorry, an error occurred while loading the content.

165805RE: [flexcoders] Function from string?

Expand Messages
  • Keith Reinfeld
    Nov 7, 2013
    • 0 Attachment

      > I would like to be able to take a
      >function name I've read from the script and call the corresponding
      >function.  

       

      Hi A. P. Damien,

      As I understand it, you have existing functions which you want to call by passing the name of a function as a string to a generic method.
      By ‘script’ I take you to mean an external text source.

      public function makeTheCall(s:String):void{
         //use try/catch to handle errors
         try {
               this[s]();
         }
         catch(e:ReferenceError) {
               //trace(s,e);
         }
      }

      >Do I have to declare the class 'dynamic' for this trick to work?
      I don’t think so.

      > Or is it always legal to do this from inside the class?
      ‘Always’ is a tricky word. The functions you want to call need to be in the same Class in order for the ‘this’ keyword to work. The function’s name and the  function name string must be an exact match for case and spelling.


      Regards,

       

      Keith Reinfeld

    • Show all 16 messages in this topic