5 Replies Latest reply on Apr 29, 2011 1:07 AM by Ankur Arora

    How To Call Dynamic Function?

    EkoBRe

      Hi,

       

      I'm trying this:

      var functionName:String = "responseListening";

      this[functionName]();

       

      public function responseListening():void {
                  Alert.show("Hello");

      }

      But I get:

           TypeError: Error #1006: value is not a function.

        • 1. Re: How To Call Dynamic Function?
          Lee Burrows Level 4

          try this:

           

          var myFunc:Function = responseListening;

          myFunc();

          1 person found this helpful
          • 2. Re: How To Call Dynamic Function?
            EkoBRe Level 1

            This works, my problem is a bit more complex.

             

            Because a name of function is dynamic too.


            In fact:

            for (var i:int = 0; i < list.length; i++ ) {
                           var functionName:String = "responseListening_" + (i + 1).toString();

                           if (this.hasOwnProperty(functionName))

                                this[functionName]();

            }

            • 3. Re: How To Call Dynamic Function?
              Lee Burrows Level 4

              hi

               

              this works for me (in cs5) so your error must be somewhere else

               

              function responseListening_1():void

              {

              trace("1");

              }

              function responseListening_2():void

              {

              trace("2");

              }

              function responseListening_3():void

              {

              trace("3");

              }

               

              for (var i:int = 0; i < 4; i++ )

              {

              var functionName:String = "responseListening_" + (i + 1).toString();

              if (this.hasOwnProperty(functionName))

              this[functionName]();

              }

              • 4. Re: How To Call Dynamic Function?
                drkstr_1 Level 4

                Not sure if this is the exact cause of the problem... but at the very least it's better practice to use strong typing...

                 

                var functionName:String = "responseListening";
                var func:Function = Function(this[functionName]);
                func();
                
                
                • 5. Re: How To Call Dynamic Function?
                  Ankur Arora Level 1

                  This code will work

                   

                  var list:Array = new Array("a1", "b1", "c1");
                  for (var i:int = 0; i < list.length; i++ ) 
                  {
                       var functionName:String = "responseListening_" + (i + 1).toString();
                       if (functionName in this)
                            this[functionName]();
                  }
                  
                  
                  public function responseListening_1():void
                  {
                  trace("a1");
                  }
                  
                  public function responseListening_2():void
                  {
                  trace("b1");
                  }