2 Replies Latest reply on Oct 27, 2006 1:48 PM by anonymous thing

    calling a function dynamically...

    DZ-015
      Perhaps I'm making this way to hard... I don't know. All I need to know how to do is call a function dynamically. For instance...

      this["functionName"];

      Obviously, that line of code doesn't work... but it should give you the idea of what I'm trying to do - call a function with a string, or some piece of data I can pass around.

      thanks!

        • 1. Re: calling a function dynamically...
          TimSymons Level 1
          When I develop a new class for buttons I use the following:

          var clickHandler:Function;

          I then assigned the "clickHandler" and a function name. When the button is clicked I execute the following code to call the function associated with clickHandler:

          this["clickHandler"](this);

          So, the key in the method that I use is to make sure to define the variable as type Function.

          You might also try the eval() method, it would probably work too.

          Tim
          • 2. Re: calling a function dynamically...
            anonymous thing
            If you need to call a function from the current scope TimSymons's hack will work "comme un charme". But, if the object is not in the current scope(like another class instance) you will probably need something like that :

            objFunction[Function(strFuncToCallBack)].apply(objFunction, arrParams)


            objFunction is an Object to callBack
            strFuncToCallBack is a String with the name of the method to callBack
            arrParams is a one dimension Array([1,2,3...]) with arguments
            you want to pass(if no arguments use [] or new Array())

            it's important to keep the cast to Function type like TimSymons said. Unfortunately, eval() doesn't work great in Flash. His behavior is really different from the Js eval() and in this case it won't work.