User Guide
46 ActionScript Language and Syntax
You should bear in mind the following when applying namespaces:
■ You can apply only one namespace to each declaration.
■ There is no way to apply a namespace attribute to more than one definition at a time. In
other words, if you want to apply your namespace to ten different functions, you must
add your namespace as an attribute to each of the ten function definitions.
■ If you apply a namespace, you cannot also specify an access control specifier because
namespaces and access control specifiers are mutually exclusive. In other words, you
cannot declare a function or property as
public, private, protected, or internal in
addition to applying your namespace.
Referencing namespaces
There is no need to explicitly reference a namespace when you use a method or property
declared with any of the access control namespaces, such as
public, private, protected,
and
internal. This is because access to these special namespaces is controlled by context. For
example, definitions placed into the
private namespace are automatically available to code
within the same class. For namespaces that you define, however, such context sensitivity does
not exist. In order to use a method or property that you have placed into a custom namespace,
you must reference the namespace.
You can reference namespaces with the
use namespace directive or you can qualify the name
with the namespace using the name qualifier (
::) punctuator. Referencing a namespace with
the
use namespace directive “opens” the namespace, so that it can apply to any identifiers
that are not qualified. For example, if you have defined the
example1 namespace, you can
access names in that namespace by using
use namespace example1:
use namespace example1;
myFunction();
You can open more than one namespace at a time. Once you open a namespace with use
namespace
, it remains open throughout the block of code in which it was opened. There is no
way to explicitly close a namespace.
Having more than one open namespace, however, increases the likelihood of name conflicts.
If you prefer not to open a namespace, you can avoid the
use namespace directive by
qualifying the method or property name with the namespace and the name qualifier
punctuator. For example, the following code shows how you can qualify the name
myFunction() with the example1 namespace:
example1::myFunction();