About Me

My photo
Derek lives in Wall Street, Lee-Over-Sands, St Osyth. A mobile and event software/product designer by trade - and is keen to improve things for all the local residents - and has lived in this idyllic location since 2009.

Tuesday, 4 May 2010

dot and bracket scope notation

Every time I try and follow programming "best practice", which of course states that you shouldn't use the old fashioned eval statement (which supposedly is inefficient), I end up looking dumbfounded by someone's decision of how they implement syntax and code... specifically Flash Actionscript 2.0 bracket notation - which for a beginner or a seasoned experienced programmer trying it out for the first time, just doesn't make a lot of sense.

Ok lets say you have 10 identical items, placed on the stage using the Flash IDE and values in each "pre-populated" and you want to do something to all instances - say read off the values - obviously you use a loop and do this - so this is supposedly bad (eval) method....

items=10;
for (item=1;item
    theObject=eval("_root.field"+item);
    trace("item "+theObject._name+" has a value of "+theObject.text);
}

In bracket notation it would be:

items=10;
for (item=1;item
    theText=_root["field"+item].text;
    trace("item "+theObject._name+" has a value of "+theObject.text);
}

it works, but is it only me who finds the fact that there is no dot after the root command a bit odd?

common sense would make you thing it should be
theText=_root.["field"+item].text;

but no - another Actionscript oddity! I remember struggling with this years ago when I had problems using an eval statement inside a dynamic element - thankfully examples on the web rescued me at the time - so today after revisiting the technique again - I thought it was worth pointing out!

No comments:

Post a Comment