AS3 Namespaces
New in ActionScript 3.0 are user defined namespaces. ActionScript 2.0 already came with built-in namespaces, public and private. These gave you the ability to, at compile time, check your code for inconsistencies in your class definitions and timeline code.
Along with more predefined namespaces (public, private, protected, and internal) ActionScript 3.0 now comes with user defined namespaces. Those familiar with lower level programming languages (such as C++/C# or Java) may already be well acquainted with these concepts. If not read on and you’ll be on your way in a couple of minutes.
Using user-define namespaces:
- Define a namespace
public namespace my_namespace;Namespaces can optionally be defined by a URI to serve as a unique identification string for that namespace. The public modifier is still required to make the namespace itself accessible externally.
- Define a method or property with the namespace
public namespace my_namespace;
class MyClass {
my_namespace myFunction() { return; }
} - Call the method or property using its namespace
use namespace my_namespace;
myFunction();or alternatively directly qualifying with:
my_namespace::myFunction();
Namespaces are generally used for avoiding name clashes and grouping logically related entities (properties or methods). Here’s a classic example of how namespaces can help make the world a better place. Say developer, your ordinary tech lead, starts a programming and creates a base class, called BaseObject. He defines a method called remove, which he decides is the method used to remove a his object from the stage, BaseObject.remove(). Developer no. 2 joins his group and adds functionality for an address book, called AddressBook. As he’s programming he decides to be smart and add a remove person function, AddressBook.remove(). As both developers talk they realize AddressBook needs to extend BaseObject, now normally we would now begin using our nifty find and replace and our tedious trial and error compiling. Here is where namespaces come to the rescue and save us from name collisions. Because who knows, it could have been possible our collisions might have occurred from utilizing code from two different frameworks or who knows, it could have been two different widget libraries. Only now we have stronger tool to create separate realms of logic.
AS3 built-in namespaces are:
public - specifies a property or method is visible to everyone
private - specifies a property or method is visible only within the object
static - specifies a property or method is inherently part of the class itself, and not the instance of the object
protected - specifies a property or method is visible within the object or other derived objects
internal -specifies a property or method is available within the same package, default assignment (previously, in AS2 public was default)
April 9th, 2007 by Stephen / 13 Comments



