开发者

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 reaso开发者_运维技巧ns why they should abandon it.

Hit me with some facts, please.


  • 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.


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.


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


A fact : I just use a similar way this afternoon.

But, below way may meet 'their' expection:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜