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;
}
大家好,这个问题我已经遇到过很多次了(其实这应该不是问题,如果我理解正确的话应该是语法设计成这样的)。
从上面的代码中,您可以看到 2 个 for 循环块,其中我为每个块声明了变量 i。我使用 Flash v.10.2 设置的 FlashDevelop 运行此代码。它提示我错误说“重复变量定义”。
我可以通过在这两个 for 循环块之外声明变量 i 并为它们重用 i 来解决这个问题。但对于我自己来说,这并不是那么干净的代码。 我的问题是
“这是actionscript的意图吗? 3 就这样吗?通过限制 变量作用域就是这样。或者可以是 修改后可以调整一些选项 对编译器或解释器来说 在这种情况下?”
提前致谢。
// 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 prompts 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第二个循环中将
i
的名称更改为j
或在 for 循环之外声明i
,所有问题都将得到解决。问题是,一旦您第一次声明
i
,它就存在于功能块的范围内,因此当您尝试在块中进一步重新声明它时,您会收到错误。顺便说一句,我个人认为在循环之外声明
i
没有任何问题。这是 AS3 中的一个范围问题,您很可能也会在许多其他 OOP 语言中发现它。
Change the name of
i
toj
in the second loop or declarei
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.