User Guide
Advanced topics 147
The AS3 namespace
The existence of two separate inheritance mechanisms, fixed property inheritance and
prototype inheritance, creates an interesting compatibility challenge with respect to the
properties and methods of the core classes. Compatibility with the ECMAScript, Edition 4
draft language specification requires the use of prototype inheritance, which means that the
properties and methods of a core class are defined on the prototype object of that class. On
the other hand, compatibility with the Flash Player API calls for the use of fixed property
inheritance, which means that the properties and methods of a core class are defined in the
class definition using the
const, var, and function keywords. Moreover, the use of fixed
properties instead of the prototype versions can lead to significant increases in run-time
performance.
ActionScript 3.0 solves this problem by using both prototype inheritance and fixed property
inheritance for the core classes. Each core class contains two sets of properties and methods.
One set is defined on the prototype object for compatibility with the ECMAScript
specification, and the other set is defined with fixed properties and the AS3 namespace for
compatibility with the Flash Player API.
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