Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 52

Programming actionscript 3.0
Table of Contents

Advertisement

If you have more than one variable to declare, you can declare them all on one line of code by
using the comma operator (
declares three variables on one line of code:
var a:int, b:int, c:int;
You can also assign values to each of the variables on the same line of code. For example, the
following code declares three variables (
var a:int = 10, b:int = 20, c:int = 30;
Although you can use the comma operator to group variable declarations into one statement,
doing so may reduce the readability of your code.
Understanding variable scope
The scope of a variable is the area of your code where the variable can be accessed by a lexical
reference. A global variable is one that is defined in all areas of your code, whereas a local
variable is one that is defined in only one part of your code. In ActionScript 3.0, variables are
always scoped to the function or class in which they are declared. A global variable is a variable
that you define outside of any function or class definition. For example, the following code
creates a global variable
shows that a global variable is available both inside and outside the function definition.
var strGlobal:String = "Global";
function scopeTest ()
{
trace(strGlobal); // Global
}
scopeTest();
trace(strGlobal); // Global
You declare a local variable by declaring the variable inside a function definition. The smallest
area of code for which you can define a local variable is a function definition. A local variable
declared within a function will exist only in that function. For example, if you declare a
variable named
str2
available outside the function.
function localScope()
{
var strLocal:String = "local";
}
localScope();
trace(strLocal); // error because strLocal is not defined globally
52
ActionScript Language and Syntax
) to separate the variables. For example, the following code
,
,
a
b,
by declaring it outside of any function. The example
strGlobal
within a function named
and
) and assigns each a value:
c
, that variable will not be
localScope()

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents