What to do instead of eval()
ActionScript 2.0 had eval(), which allowed you to execute arbitrary ActionScript. ActionScript 3.0 doesn’t have it. I’m accustomed to being able to execute arbitrary JavaScript generated at runtime. It’s a pretty powerful feature, and it’s hard to give it up. I think I understand the rationale for getting rid of it (mostly for performance), but what to do instead?
Well, it turns out that one thing you can do that is really better in some ways is to call a method whose name is generated as a string at runtime. This gets you the same sort of dynamic execution, but it’s a lot more predictable and less chaotic.
To do this, you need to include the keyword “dynamic” as part of your class declaration, e.g., public dynamic class Foo. Then to make the call, just say:
var functionName:String = "foo" + bar; if (this.hasOwnProperty(functionName)) this[functionName]();
Neat!




Recent Comments