View comments | RSS feed

_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, 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.

ReturnsA reference to the global object that holds the core ActionScript classes, such as String, Object, Math, and Array.

Availability: ActionScript 1.0; Flash Player 6

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);
 }
}

trace(factorial(1)); // 1
trace(factorial(2)); // 2
trace(factorial(3)); // 6
trace(factorial(4)); // 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 = "globalVariable";
trace(_global.myVar); // globalVariable
trace(myVar); // globalVariable

myVar = "localVariable";
trace(_global.myVar); // globalVariable
trace(myVar); // localVariable

See also

var statement, set variable statement


Version 8

Comments


j_cutter said on Mar 15, 2006 at 1:59 PM :
This must have been noted before...

declaring a global var works ok as follows:
_global.g_MSG_string = "Global Var declared with _global";

fails as follows:
_global.g_MSG_string:String = "Global Var declared with _global";
>> **Error** Scene=Scene 1, layer=actions, frame=1:Line 1: Syntax error.
_global.g_MSG_string:String = "Global Var declared with _global";<<

Apparently _global vars are still untyped.
ockley said on May 28, 2006 at 4:19 AM :
Jep, youre right. the _global object is an allready defined object. It contains the core class definitions of classes like String, Array etc. You can extend the _global object with your own functions and variables by adding to the _global object, just as you would do, with a normal object.

When working with objects in Flash you cannot define variables strictly using semicolon.
juankpro said on Aug 1, 2007 at 9:50 AM :
It is not that the _global variables are untyped. What happens is that when calling _global.identifier you are just adding a variable to an existent object, like adding a property to a movie clip mc.identifier. So this statement is not a declararion, it is just an assignment to a property. Only declarations can use the AS 2.0 data typing notation.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001786.html