2 Replies Latest reply on Jan 1, 2008 1:58 AM by Greg Dove

    Execute string as a function

      Hello Friends,
      Can anyone guide me through this...
      I want to execute a string as as a normal statement line...
      for example...

      var str:String = "trace('done')";
      function abc() {

      I want this code to work as:

      function abc() {

      Please help me guys, I am in trouble...
      Your help will be highly appreciated...
      Thanks in anticipation...

      Looking for your reply...
        • 1. Re: Execute string as a function
          kglad Adobe Community Professional & MVP
          i don't think you'll be able to use a reserved function, but for a user defined function you should be able to use:

          • 2. Execute string as a function
            Greg Dove Level 4
            You can't do this like you would in javascript, e.g. with javascript's eval function, but the limited actionscript eval function could be put to some use here. You would need to split the string out into scope, method and arguments, in a similar way to following line in kglad's example above.

            this[str]("test string");

            The folllowing code assumes that the split to separate the arguments out into an array has been made. (trace only has one argument, so its not a great example - there's a couple of others included). Its only an more generalised extension of the approach kglad used... and I found that many of the functions listed as being in global scope didn't seem to work. It's definitely just a starting point and not a full solution.