Namespaces give you control over the visibility of the properties and methods that you create. The public, private, protected and internal access control specifiers can be thought of as built-in namespaces. If these predefined access control specifiers do not suit your needs, you can create your own namespaces.
If you are familiar with XML namespaces, much of this discussion will not be new to you, though the syntax and details of the ActionScript implementation are slightly different from those of XML. If you have never worked with namespaces before, the concept itself is straightforward, but the implementation has specific terminology that you will need to learn.
To understand how namespaces work, it helps to know that the name of a property or method always contains two parts: an identifier and a namespace. The identifier is what you generally think of as a name. For example, the identifiers in the following class definition are sampleGreeting and sampleFunction.
class SampleCode {Whenever identifiers are not preceded by a namespace qualifier, they are qualified by the default internal namespace, which means they are visible only to callers in the same package. To ensure that an identifier is available everywhere, you must specifically precede the identifier name with the public attribute. In the previous example code, both sampleGreeting and sampleFunction have a namespace value of internal.
var sampleGreeting:String;
function sampleFunction () {
trace (sampleGreeting + " from sampleFunction()");
}
}
There are three basic steps to follow when using namespaces. First, you must define the namespace using the namespace keyword. For example, the following code defines the version1 namespace.
namespace version1;
Second, you apply your namespace by using it instead of an access control specifier in a property or method declaration. The following example places a function named myFunction into the version1 namespace.
version1 function myFunction () {}
Third, you can then reference the namespace with the use directive or by qualifying the name of an identifier with a namespace. The following example references the myFunction() function through the use directive.
use namespace version1;
myFunction();
You can also use a qualified name to reference the myFunction() function.
version1::myFunction();
0 komentar:
Post a Comment