Skip to main content
Participant
January 1, 2008
Question

Execute string as a function

  • January 1, 2008
  • 1 reply
  • 220 views
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() {
str;
}

I want this code to work as:

function abc() {
trace('done');
}


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

Looking for your reply...
This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 1, 2008
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:

Inspiring
January 1, 2008
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.