一个神秘错误的故事 Flex/ Actionscript
突然间,Flex 似乎不喜欢变量声明了。例如,我写(在 mxml 组件的脚本部分)
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
var i:int = 1;
while(i< 9) i++;
[Bindable]
public var evolution:ArrayCollection = new ArrayCollection();
]]>
</mx:Script>
并且它表示变量 i 尚未定义。这对我来说没有任何意义。猜猜可能出了什么问题吗?当我将进化 ArrayCollection 调用不带参数的简单构造函数时,它突然发生了。我想使用 while 循环来添加项目,但现在我已经删除了几乎所有代码,而且我无法弄清楚出了什么问题,它似乎不再识别我的变量了!我要疯了。
Suddenly Flex seems to dislike variable declaration. For example I write (on the script part of a mxml component)
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
var i:int = 1;
while(i< 9) i++;
[Bindable]
public var evolution:ArrayCollection = new ArrayCollection();
]]>
</mx:Script>
And it says the variable i has not been defined. This doesn't make any sense to me. Any guess of what might have gone wrong? It happened all of a sudden, when I put the evolution ArrayCollection calling the simple constructor with no arguments. I wanted to add items using a while cycle instead, but now I've erased nearly all the code and I can't figure out what went wrong it doesn't seem to recognize my variables anymore! I'm going crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果将循环包装在函数中,则可以解决此问题。
事实上,每当您尝试在函数之外运行代码时,您都会收到这样的错误。
例如,如果您添加了一些设置进化ArrayCollection的.source属性的代码,如下所示:
那么您将在该行收到一个错误,告诉您“进化”未定义。
希望有帮助。
If you wrap your loop in a function, you'll get past this issue.
As a matter of fact, anytime you try to run code outside of a function you'll get an error like this.
For example, if you added some code setting the .source property of the evolution ArrayCollection, like so:
then you will get an error at that line telling you that 'evolution' is undefined.
Hope that helps.
它并不是告诉您变量 i 未定义,而是告诉您属性 i 未定义。
我认为您不能在实际函数之外运行 while 循环。事实上,也没有理由。如果您需要立即运行该循环,可以将其放入初始化函数中。
It's not telling you that variable i is not defined, it's telling you that the property i is not defined.
I don't think you can run that while loop outside of an actual function. And really, there wouldn't be a reason too. If you need to run that loop immediately you can put it in the initialize function.
虽然在 mxml 文件中,您会看到很多 xml 标签,但是当 mxml 文件被编译时,它会被翻译成一个类。因此,不可能只在类中编写一些不在函数中的代码。
Although in the mxml file, you see a lot of xml tags, but when the mxml file is being compiled, it gets translated into a class. Therefore, it's not possible to just write some code in the class that isn't in a function.