actionscript-3 - 这怎么可能是输出?
解决方案
所以,我们有 3 种语法:
// 1. Variable declaration.
var a:int;
// 2. Assign value to variable.
a = 0;
// 3. Declare variable and assign value in one go.
var b:int = 1;
棘手的时刻是在 AS3 中的变量声明不是一个操作。它是一种结构,它告诉编译器您将在给定的上下文中使用具有特定名称和类型的变量(作为类成员或时间线变量或方法内的局部变量)。从字面上看,您在代码中声明变量的位置并不重要。我必须承认,从这个角度来看,AS3 是丑陋的。以下代码可能看起来很奇怪,但在语法上是正确的。让我们阅读并理解它的作用和原因。
// As long as they are declared anywhere,
// you can access these wherever you want.
i = 0;
a = 0;
b = -1;
// The 'for' loop allows a single variable declaration
// within its parentheses. It is not mandatory that
// declared variable is an actual loop iterator.
for (var a:int; i <= 10; i++)
{
// Will trace lines of 0 0 -1 then 1 1 0 then 2 2 1 and so on.
trace(a, i, b);
// You can declare a variable inside the loop, why not?
// The only thing that actually matters is that you assign
// the 'a' value to it before you increment the 'a' variable,
// so the 'b' variable will always be one step behind the 'a'.
var b:int = a;
a++;
}
// Variable declaration. You can actually put
// those even after the 'return' statement.
var i:int;
让我再说一遍。您声明变量的位置无关紧要,只是您所做的事实。声明变量不是操作。但是,赋值是。您的代码实际上如下所示:
function bringMe(e:Event):void
{
// Lets explicitly declare variables so that assigning
// operations will come out into the open.
var i:int;
var score:int;
for (i = 1; i <= 10; i++)
{
// Without the confusing declaration it is
// obvious now what's going on here.
score = 0;
score++;
// Always outputs 1.
trace(score);
// Outputs values from 1 to 10 inclusive, as expected.
trace(i);
}
}
推荐阅读
- java - 如何识别 JFR 事件类型
- photoshop - 如何在 Photoshop 中编写间隔脚本
- css - 如何在闪亮的应用程序中更改使用 DT 库生成的表格标题的颜色?
- reactjs - 如何用纱线解决依赖项安装问题?
- node.js - 我正在创建 appInsights 的 NPM 包。即使是从配置传递的密钥仍然出现错误
- css - 如何仅在 wordpress 中使用 css 使响应式菜单上的子项始终对移动设备可见
- python-3.x - 如何使用 Python 的 Kubernetes 库删除节点污点
- android - 从 Git 克隆时找不到点 JKS(.jks 文件)
- node.js - 配置 elastic beanstalk 以安装开发工具
- postgresql - 调用 prostresql 过程 nestjs/typeorm 时出错