Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Data and Data Types > About variables > About variables and scope > Local variables | |||
When you use the var statement inside a function block, you declare local variables. When you declare a local variable within a function block (also called function definition), it is defined within the scope of the function block, and expires at the end of the function block. Therefore, the local variable only exists within that function.
For example, if you declare a variable named myStr within a function named localScope, that variable will not be available outside of the function.
function localScope():Void {
var myStr:String = "local";
}
localScope();
trace(myStr); // Undefined, because myStr is not defined globally
If the variable name you use for your local variable is already declared as a timeline variable, the local definition takes precedence over the timeline definition while the local variable is in scope. The timeline variable will still exist outside of the function. For example, the following code creates a timeline string variable named str1, and then creates a local variable of the same name inside the scopeTest() function. The trace statement inside the function generates the local definition of the variable, but the trace statement outside the function generates the timeline definition of the variable.
var str1:String = "Timeline";
function scopeTest():Void {
var str1:String = "Local";
trace(str1); // Local
}
scopeTest();
trace(str1); // Timeline
In the next example, you can see how certain variables live only for the life of a specific function and can generate errors if you try to refer to the variable outside the scope of that function.
function sayHello(nameStr:String):Void {
var greetingStr:String = "Hello, " + nameStr;
trace(greetingStr);
}
sayHello("world"); // Hello, world
trace(nameStr); // undefined
trace(greetingStr); // undefined
Flash displays the string "Hello, world" in the Output panel and displays undefined for the values of nameStr and greetingStr because the variables are no longer available in the current scope. You can only reference nameStr and greetingStr in the execution of the sayHello function. When the function exits, the variables cease to exist.
The variables i and j are often used as loop counters. In the following example, you use i as a local variable; it exists only inside the initArray() function:
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
|
NOTE |
|
It's also common to see the following syntax for a |
This example displays undefined in the Flash test environment because the variable i isn't defined in the main timeline. It exists only in the initArray() function.
You can use local variables to help prevent name conflicts, which can cause unexpected results in your application. For example, if you use age as a local variable, you could use it to store a 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 About assigning data types and strict data typing.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000665.html