The variables
and
i
j
local variable; it exists only inside the
var myArr:Array = new Array();
function initArray(arrayLength:Number):Void {
var i:Number;
for(i = 0; i < arrayLength; i++) {
myArr[i] = i + 1;
}
}
trace(myArr); // <blank>
initArray(3);
trace(myArr); // 1,2,3
trace(i); // undefined
It's also common to see the following syntax for a for loop: for (var i:Number = 0;
i < arrayLength; i++) {...}.
This example displays
defined in the main timeline. It exists only in the
You can use local variables to help prevent name conflicts, which can cause unexpected results
in your application. For example, if you use
person's age in one context and the age of a person's child in another context. There is no
conflict in this situation because you are using these variables in separate scopes.
It's good practice to use local variables in the body of a function so the function can act as an
independent piece of code. You can change a local variable only within its own block of code.
If an expression in a function uses a global variable, code or events outside the function can
change its value, which changes the function.
You can assign a data type to a local variable when you declare it, which helps prevent
assigning the wrong type of data to an existing variable. For more information, see
assigning data types and strict data typing" on page
About loading variables
In the following sections, you load variables from the server in different ways or into a
document from a URL string or FlashVars (you can use FlashVars to pass variables into Flash)
in your HTML code. These practices demonstrate that there are several ways to use variables
outside a SWF file.
You can find more information on loading variables (such as name/value pairs) in
"Working with External Data," on page
100
Data and Data Types
are often used as loop counters. In the following example, you use
initArray()
in the Flash test environment because the variable
undefined
633.
function:
function.
initArray()
as a local variable, you could use it to store a
age
81.
as a
i
isn't
i
"About
Chapter 16,
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?
Questions and answers