
104
ActionScript language elements
_global property
_global.identifier
A reference to the global object that holds the core ActionScript classes, such as String,
Object, Math, and Array. For example, you could create a library that is exposed as a global
ActionScript object, similar to the Math or Date object. Unlike Timeline-declared or locally
declared variables and functions, global variables and functions are visible to every timeline
and 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, for instance, _global.variableName. Failure to do so creates a local variable of the
same name that obscures the global variable you are attempting to set.
Returns
A reference to the global object that holds the core ActionScript classes, such as
String, Object, Math, and Array.
Availability:
ActionScript 1.0; Flash Lite 2.0
Example
The following example creates a top-level function,
factorial(),
that is available to every
timeline and scope 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: global
myVar = "local";
trace("_global.myVar: " + _global.myVar); // _global.myVar: global
trace("myVar: " + myVar); // myVar: local
See also
var statement
,
set variable statement
Summary of Contents for Flash Lite 2
Page 1: ...Flash Lite 2 x ActionScript Language Reference...
Page 22: ...22 Contents...
Page 244: ...244 ActionScript language elements...
Page 760: ...760 ActionScript classes...