actionscript-3 - 重复变量定义的后果是什么?

标签 actionscript-3

我的代码如下所示:

var variableX:uint = something;    

if (variableX > 1)
{
  var variableY:uint = foo;
}
else
{
  var variableY:uint = bar;
}

在FlashDevelop中编译时,编译器给出以下警告:

Warning: Duplicate variable definition.

作为 AS3 和编程的初学者,我不喜欢编译器警告。编译器眯着眼睛看着我说:“好吧,伙计,这次我就放过你了。但我警告你!”然后不告诉我我正在做的事情有什么问题。

做这样的事情时我应该注意什么?我的意思是我显然可以在 if 之外定义变量,那么这就不会成为问题,但也许还有更多的事情?或者编译器只是给出了一个有用的提示“嘿,您可能不小心创建了两个具有相同名称的不同变量”?

最佳答案

您对警告的评估是正确的。它只是让您知道范围内已经有一个具有该名称的变量,并且您将重新定义它。这样您就不会意外覆盖变量。尽管它们可能看起来不在同一范围内,但如果您查看此页面上的变量提升,您将看到具体内容: http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html

An interesting implication of the lack of block-level scope is that you can read or write to a variable before it is declared, as long as it is declared before the function ends. This is because of a technique called hoisting , which means that the compiler moves all variable declarations to the top of the function. For example, the following code compiles even though the initial trace() function for the num variable happens before the num variable is declared:

我个人的倾向是自己将定义放在首位,以避免出现额外的警告,使我错过更重要的问题。已经脱离 AS3 一段时间了,但在大型项目中,人们会放任不管,最终会出现 100-1000 条警告,而相关警告则被埋没。

关于actionscript-3 - 重复变量定义的后果是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17330366/

相关文章:

c++ - 使用套接字从 Actionscript3 调用 native 函数

apache-flex - 如何从 Flash/Flex/AIR 运行 EXE 文件?

actionscript-3 - 在AS3中在循环中或循环前声明变量?

html - 在 Flash 页面中编辑分辨率

actionscript-3 - 用于监视从显示列表中添加/删除 MovieClips 的事件?

c++ - 在 AIR Native Extension 中包含库会导致错误,所有方法都为 "The extension context does not have a method with the name..."

actionscript-3 - 游戏 session 声音播放/录音

flash - AS3 类/基类构造函数

actionscript-3 - 有没有办法使用 Flash 中的 slider 组件来控制 FLVPlayback 组件的音量?

flash - 最好的 Flash 音频/视频 + 交互服务器?