O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Open Namespaces and the use namespace Directive

Remember the simple Items class from Example 17-1?

package {
  public class Items {
    fruit var orange:String = "Round citrus fruit";
    color var orange:String = "Color obtained by mixing red and yellow";

    public function Items () {
      trace(fruit::orange);
      trace(color::orange);
    }
  }
}

As we learned earlier, one way to access the orange variables in the preceding code is to use qualified identifiers, as in:

trace(fruit::orange); // Displays: Round citrus fruit
trace(color::orange); // Displays: Color obtained by
                      //           mixing red and yellow

But, for the sake of convenience, ActionScript also provides another tool for accessing variables qualified by a namespace: the use namespace directive. The use namespace directive adds a given namespace to the so-called open namespaces for a particular scope of a program. The open namespaces is the set of namespaces ActionScript consults when attempting to resolve unqualified references. For example, if namespace n is in the open namespaces, and ActionScript encounters an unqualified reference to a variable p, then ActionScript will automatically check for the existence of n::p.

Here's the general form of the use namespace directive:

use namespace namespaceIdentifier

where namespaceIdentifier is the namespace identifier that should be added to the set of open namespaces. Note that namespaceIdentifier must be a compile-time constant, so it cannot be a variable that references a namespace value.

Let's see how use namespace ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required