Using a Namespace to Qualify Variable and Method Definitions

Now that we've defined the fruit and color namespaces, we can use them to specify the so-called qualifier namespace for new methods and variables. The qualifier namespace is the namespace within which the variable or method name is unique.

To specify the qualifier namespace for a new variable or method, we use that namespace's identifier as an attribute of the variable or method definition, as follows:

// Specify the qualifier namespace for a variable
namespaceIdentifier var propName:datatype =

// Specify the qualifier namespace for a method
namespaceIdentifier function methodName (params):ReturnType

The following rules apply to the namespaceIdentifier:

  • It must be accessible to the scope where the variable or method is defined, as discussed later in the section "Namespace Accessibility."

  • It cannot be a literal value; specifically it cannot be a string literal containing a namespace name (URI).

  • It must be a compile-time constant.

These three rules effectively mean that the namespaceIdentifier can only be a namespace identifier created with the namespace keyword, and specifically cannot be a variable that refers to a namespace value. (We'll learn about variables that refer to namespace values later in the section "Assigning and Passing Namespace Values.")

Here's how to specify the qualifier namespace fruit for an instance variable named orange:

fruit var orange:String = "Round citrus fruit";

Get Essential ActionScript 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.