Friday, July 27, 2007

AS3 namespace

The AS3 namespace provides a convenient mechanism for choosing between the two sets of properties and methods. If you do not use the AS3 namespace, an instance of a core class inherits the properties and methods defined on the core class's prototype object. If you decide to use the AS3 namespace, an instance of a core class inherits the AS3 versions because fixed properties are always preferred over prototype properties. In other words, whenever a fixed property is available, it is always used instead of an identically named prototype property.

You can selectively use the AS3 namespace version of a property or method by qualifying it with the AS3 namespace. For example, the following code uses the AS3 version of the Array.pop() method:

var nums:Array = new Array(1, 2, 3);
nums.AS3::pop();
trace(nums); // output: 1,2

Alternatively, you can use the use namespace directive to open the AS3 namespace for all the definitions within a block of code. For example, the following code uses the use namespace directive to open the AS3 namespace for both the pop() and push() methods:

use namespace AS3;

var nums:Array = new Array(1, 2, 3);
nums.pop();
nums.push(5);
trace(nums) // output: 1,2,5

ActionScript 3.0 also provides compiler options for each set of properties so that you can apply the AS3 namespace to your entire program. The -as3 compiler option represents the AS3 namespace, and the -es compiler option represents the prototype inheritance option (es stands for ECMAScript). To open the AS3 namespace for your entire program, set the -as3 compiler option to true, and the -es compiler option to false. To use the prototype versions, set the compiler options to the opposite values. The default compiler settings for Adobe Flex Builder 2 are -as3 = true and -es = false.