for (const i) 而不是 for (var i)
for (const i) rather than for (var i)
虽然智能代码完成似乎没有得到它(至少在 Flash Builder 中),编译器似乎确实允许它并且定义一个 for迭代器变量作为常量。
for (const i:int = 0; i < 100; ++i) {
i = 5; // this breaks the loop's logic, but is caught by the compiler
}
你不能改变const
的值,const
指的是Constants, properties that can never change. You cannot increment a const
, you can only assign to a const
once. After that point, it is fixed until it is disposed. A const
is a poor choice as a counter in a for loop because you can not change it's value. You can learn more about actionscripts implementation of const
here的编程概念。
根据此 document 遗留编译器中存在允许增量运算符在 const
上工作的错误,此错误应该已在 Actionscript Complier 2.0 中解决,如果此代码有效,最有可能的解释是您使用的是旧版编译器。
如果您使用的是 ASC 2.0 或更高版本,我可以告诉您的是这种行为与 Actionscript 3 language rules 相矛盾,您不应该依赖它。这很可能是 Adobe 随心所欲修复的错误。
虽然智能代码完成似乎没有得到它(至少在 Flash Builder 中),编译器似乎确实允许它并且定义一个 for迭代器变量作为常量。
for (const i:int = 0; i < 100; ++i) {
i = 5; // this breaks the loop's logic, but is caught by the compiler
}
你不能改变const
的值,const
指的是Constants, properties that can never change. You cannot increment a const
, you can only assign to a const
once. After that point, it is fixed until it is disposed. A const
is a poor choice as a counter in a for loop because you can not change it's value. You can learn more about actionscripts implementation of const
here的编程概念。
根据此 document 遗留编译器中存在允许增量运算符在 const
上工作的错误,此错误应该已在 Actionscript Complier 2.0 中解决,如果此代码有效,最有可能的解释是您使用的是旧版编译器。
如果您使用的是 ASC 2.0 或更高版本,我可以告诉您的是这种行为与 Actionscript 3 language rules 相矛盾,您不应该依赖它。这很可能是 Adobe 随心所欲修复的错误。