Dynamically Referencing Variables and Methods in ActionScript

You are here

Sometimes, the state of an object decides which method has to be executed. Instead of an unwieldy switch construction like this …

switch (task) {

case "sleep":
sleep();
break;

case "walk":
walk();
break;

}

… you can use the someObject[someExpression] notation. This works for any object, and you can use it to access both variables and methods. All those lines from above become this:

thistask;

Much faster. Only problem I have yet to figure out: what happens, when there is no method by this name?

/