Friday, January 25, 2008

Cute EcmaScript

The following is a nifty trick that works in JavaScript, but you should be able to tweak it slightly to work in ActionScript as it leverages EcmaScript features.


function Train(){
this.name = "Choo Choo";
this.show = function(){
return this.name;
};
};
var engine = new Train;
engine.name = "Orient Express";
function go(){
alert(engine["show"].call(engine));
}


What is cool is that you can access a function just like any other property of the object. So the go function could take a parameter that would be the name of the function to call on the engine object. It's like reflection, only better.

No comments: