« Tree component | Inicio | Flexibility matters (AS2) »

Flexibility matters (AS1)

If we want to get or set any of the properties of an object, we can write something like this:

var miObj = new Object(); miObj.prop1 = "design-nation";

Although, we could have written something like:

var miObj = new Object(); miObj["prop1"] = "design-nation";

So, we can access an object properties dynamically, for example:

var miObj = new Object(); miObj.prop1 = "property1"; miObj.prop2 = "property2"; miObj.prop3 = "property3";

And we can retrieve those values like this:

for (var k=1;k<4;k++){ trace("prop"+k+ " = " + miObj["prop"+k]); }

But, what about the methods?. No problem. Let’s see:

var miObj = new Object(); miObj.metodo1 = function(){ trace("metodo1"); } miObj["metodo1"]();

That’s the way we can execute any given method of an object, without really knowing which one we are trying to access.