_global object
Availability
Flash Player 6.
Usage
_global.identifier
Parameters
None.
Returns
A reference to the global object that holds the core ActionScript classes, such as String, Object,
Math, and Array.
Description
Identifier; creates global variables, objects, or classes. For example, you could create a library that
is exposed as a global ActionScript object, similar to the Math or Date object. Unlike locally
declared variables and functions, global variables and functions are visible to every scope in the
SWF file, provided they are not obscured by identifiers with the same names in inner scopes.
Note: When setting the value of a global variable, you must use the fully qualified name of the
variable, e.g. _global.variableName. Failure to do so will create a local variable of the same name
that obscures the global variable you are attempting to set.
Example
The following example creates a top-level function
in a SWF file:
_global.factorial = function(n:Number) {
if (n<=1) {
return 1;
} else {
return n*factorial(n-1);
}
};
// Note: factorial 4 == 4*3*2*1 == 24
trace(factorial(4)); // output: 24
The following example shows how the failure to use the fully qualified variable name when setting
the value of a global variable leads to unexpected results:
_global.myVar = "global";
trace("_global.myVar: " + _global.myVar); // _global.myVar: global
trace("myVar: " + myVar);
myVar = "local";
trace("_global.myVar: " + _global.myVar); // _global.myVar: global
trace("myVar: " + myVar);
See also
var,
set variable
ActionScript Core Language Elements
factorial()
// myVar: global
// myVar: local
CHAPTER 5
that is available to every scope
_global object
171
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?