This is the syntax ["instankename"]

I see that my employees use this awful 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 } 

Since I'm the boss of sod, I am compiling a list of reasons why they should use arrays for iteration rather than the nasty square syntax. I know that it is wrong to use this syntax, but I cannot come up with enough convincing reasons why they should refuse it.

Hit me with some facts, please.

+6
flash actionscript-3
source share
4 answers
  • Using an array makes it easy to add another element. You do not need to declare a new variable.
  • You do not need to change the limit of the for loop when adding another element.
  • You have only one variable to rename if necessary.
  • It helps you think fruitfully when you can recognize a collection of things with a semantically useful definition.
+8
source share

Type-security.

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

Seriously, the type system in AS3 exists for some reason. Use it. Don't figure out ways around this. This prevents problems.

+6
source share

I will argue with you here and say that you should not use arrays, but vectors .

+4
source share

Fact: I just use a similar path this afternoon.

But below can be found "their" atonement:

var mcs: Array = [mc1, mc2, mc3, mc4, mc5];

for (var i: int = 1; i <= mcs.length; i ++)
{
AddChild (MCS [I]);
TweenLite.to (mcs [i], 1, {alpha: 0});
}

0
source share

All Articles