Apollo Cheatsheet’s up! →← Adobe releases Apollo public alpha

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

Comments on “AS3 Namespaces”

  1. oh thanks for this! my brain is very resistive to move onto AS3 programming, so posts like this help! I used to code in C++/Java but never really get the OOP concept as a whole.

    Og2t on April 10th, 2007 at 4:07 am
  2. […] read more… […]

    trace(chris.foster) » BIG SPACESHIP LABS / » AS3 Namespaces on April 10th, 2007 at 4:18 am
  3. […] Henry have a post to talk about the usage of namespaces in as3. he have a example to talk about how to use namespace to solve the function name’s collisions on class inheritance. i think it is not a good solution for that problem, it is any other way to use of namespaces. […]

    as3 namespaces … how to use it better? - danielyuen.hk Blog on April 10th, 2007 at 4:46 am
  4. […] I right this blog after reading about the introduction of namespaces http://labs.bigspaceship.com/blog/?p=33 in as3, which although valuable will surely pose confusion with the keyword package. […]

    flashMonkey » Blog Archive » Superfluous keyword ‘package’ in as3 on April 10th, 2007 at 4:52 am
  5. […] Ein paar Beispiele wie man die Namespaces in as3 verwendet, um Namenskollisionen bei Vererbung zu verhindern. […]

    AS3 Namespaces at flash und so on April 10th, 2007 at 5:53 am
  6. flashMonkey, packages and namespaces perform different functions programmatically. of course, you don’t have to use anything the way it was intended, but typically I would suggest using packages to create code libraries and namespaces to separate code entities.

    Henry on April 10th, 2007 at 3:14 pm
  7. I could be nuts but isnt the appropriate name for these “accessors” or “access levels”? Since “namespace” usually refers to the package (com.bigspaceship.confusion.*).

    foobar on April 18th, 2007 at 3:11 pm
  8. Actually the proper name for these are “Access Control Modifiers” … calling them namespaces is just gunna confuse the hell outta everyone.

    Anonymous on May 2nd, 2007 at 9:18 pm
  9. Nope sorry. They’re actually namespaces. Essential ActionScript 3.0 will clear this up if you have more questions.

    Henry on June 7th, 2007 at 10:12 pm
  10. ‘access control modifiers’ allow you to modify the access to your control, like public, private, protected, internal do.

    and yes ‘namespaces’ and packages are very close concepts. i agree with henry about the usage in general.

    kzm on August 8th, 2007 at 5:16 pm
  11. calling these namespace is stupidity defined. they are access modifiers

    chewy on January 8th, 2008 at 5:21 pm
  12. I am reading the essential actionscript 3.0 namespace chapter. I am sick of it since its purpose is not clear (or confusing).

    Maybe named it “access modifier” as some sugguested will motivate me to learn it.

    Anyway, for now just skip this chapter.

    york on January 29th, 2008 at 6:26 pm
  13. just found this:
    http://flexblog.faratasystems.com/?p=115

    the “namespace” is actually “custom access level”.

    the “namespace” is the first (hopefully the only) stupid concept making me become resistent in learning as3.

    pick up hte book and re-read chapter 17 from where I left

    york on January 29th, 2008 at 6:48 pm