开发者

Scope of variable questions in AS3

        // content loaders
        if (_contentLoaders != null)
        {
            // iterate through all the content loaders then dispose it
            for (var i:int = 0; i < _contentLoaders.length; i++)
            {
                _contentLoaders[i].dispose();

            }
            _contentLoaders.splice(0, _contentLoaders.length);
            _contentLoaders = null;
        }

        // text content loaders
        if (_textContentLoaders != null)
        {
            // iterate through all the text content loaders then dispose it
            for (var i:int = 0; i < _textContentLoaders.length; i++)
            {
                _textContentLoaders[i].dispose();
            }
            _textContentLoaders.splice(0, _textContentLoaders.length);
            _textContentLoaders = null;
        }

Hello guys, I have come across many times about this problem (in fact it should not be a problem, if I understand correctly it should be a design of syntax to be like that way).

From the code above, you see 2 block of for-loop in which you see I declare variable i for each block. I run this code with FlashDevelop set up with Flash v.10.2. It prompt开发者_运维问答s me error saying that "Duplicate variable definition".

I could solve this problem by declare variable i outside these 2 for-loop block, and reuse i for both of them. But for myself, this is not so clean of code. My question is

"Is this an intention of actionscript 3 to be like that way ? by limitting variable scope this way. Or can it be modified can tune up with some option to the compiler or say interpreter as in this case ?"

Thanks in advance.


Change the name of i to j in the second loop or declare i outside of the for loops and all your problems will be resolved.

The issue is that once you declare i the first time, it is existing in the scope of the function block, so when you try to redeclare it further down in the block you are getting the error.

I personally don't see anything wrong with declaring i outside of the loops by the way.

This is a scope issue in AS3, and you will most likely find it in many other OOP languages as well.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜