Apr 09, 2007

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)

Share this Post


                           

Comments


Ron     Jul 26, 2009
Your example doesn't show the need for namespaces. The adressBook class can override the remove() functoin and avoid name clashes more easily

john doe     Oct 28, 2008
protected: what are objects derived from it? subclasses?

york     Jan 29, 2008
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     Jan 29, 2008
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.

chewy     Jan 08, 2008
calling these namespace is stupidity defined. they are access modifiers

kzm     Aug 08, 2007
'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.

Henry     Jun 07, 2007
Nope sorry. They're actually namespaces. Essential ActionScript 3.0 will clear this up if you have more questions.

Anonymous     May 02, 2007
Actually the proper name for these are "Access Control Modifiers" ... calling them namespaces is just gunna confuse the hell outta everyone.

foobar     Apr 18, 2007
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.*).

Henry     Apr 10, 2007
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.

AS3 Namespaces at flash und so     Apr 10, 2007
[...] Ein paar Beispiele wie man die Namespaces in as3 verwendet, um Namenskollisionen bei Vererbung zu verhindern. [...]

flashMonkey » Blog Archive » Superfluous keyword ‘package’ in as3     Apr 10, 2007
[...] 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. [...]

as3 namespaces … how to use it better? - danielyuen.hk Blog     Apr 10, 2007
[...] 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. [...]

trace(chris.foster) » BIG SPACESHIP LABS / » AS3 Namespaces     Apr 10, 2007
[...] read more… [...]

Og2t     Apr 10, 2007
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.


Speak






Submit »