Sometimes, the state of an object decides which method has to be executed. Instead of an unwieldy
switch construction like this …
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:
Much faster. Only problem I have yet to figure out: what happens, when there is no method by this name?