Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

this["instancename"] syntax

I see my co-workers use this horrid syntax a lot:

var mc1: MovieClip;
var mc2: MovieClip;
var mc3: MovieClip;
var mc4: MovieClip;
var mc5: MovieClip;

for (var i:int = 1; i <= 5; i++) { 
   addChild(this["mc" + i]); // UURRGGHHH
   TweenLite.to(this["mc"+i], 1, {alpha: 0}); // FNNNGGGGGHHHH
}

Because I'm a bossy sod, I'm putting together a list of reasons why they should use arrays for iteration, not nasty square-bracket syntax. I know it's wrong to use that syntax but I can't think of enough compelling reasons why they should abandon it.

Hit me with some facts, please.

like image 951
hamishtaplin Avatar asked Dec 09 '10 14:12

hamishtaplin


3 Answers

  • Using an array makes it easy to add another item. You don't have to declare a new variable.
  • You don't have to modify the limit of your for loop when you add another item.
  • You only have one variable to rename if that need arises.
  • It helps you think in a fruitful manner when you can recognize a collection of things by a semantically useful definition.
like image 100
Ishmael Avatar answered Oct 05 '22 05:10

Ishmael


Type-safety.

The square-bracket syntax will not result in the a decent type so that the compiler can check for problems at compile time. I show no mercy to people who come to me with runtime exceptions caused by type problems.

Seriously, the type system in AS3 is there for a reason. Use it. Don't figure out ways to work around it. It prevents problems.

like image 43
mpdonadio Avatar answered Oct 05 '22 04:10

mpdonadio


I'll argue with you here, and say that you shouldn't be using arrays, but vectors.

like image 33
Daniel Avatar answered Oct 05 '22 04:10

Daniel