Flex Tip 3: Using Namespaces

January 21, 2008 – 12:35 pm

Namespaces are used in ActionScript 3 to limit the scope of custom methods / properties / constants within classes. The most known namespaces are public, private, protected, internal thought you might know them not as namespaces but as keywords. Besides those you can create your custom namespace to use with your classes. Lets get our hands into an example so you can see it working. On almost every project you will need to handle things differently in testing environment and release environment and this is what we will do.

Lets first build two namespaces. Namespaces are placed in .as files, .as files that have same name as the namespace. Here is the code for both.


In tip3.namespaces.testing.as:

1
2
3
4
package tip3.namespaces
{
    public namespace testing = "http://tip3/namespaces/testing";
}

In tip3.namespaces.release.as:

1
2
3
4
package tip3.namespaces
{
    public namespace release = "http://tip3/namespaces/release";
}

Now lets see the class that uses the two namespaces. Class is tip3.TestNamespaces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package tip3
{

import mx.controls.Alert;

import tip3.namespaces.*;

public class TestNamespaces
{
    testing static function alert():void
    {
        Alert.show( "Alert using test environment" );
    }
   
    release static function alert():void
    {
        Alert.show( "Alert using release environment" );
    }
}

}

First see the import tip3.namespaces.*; that loads all desired namespaces from their package. Inside the class we have 2 methods but if you look closely, in fact is just one working in 2 namespaces, first one “testing” and the second one “release”. Also see how the custom namespaces replace keywords such as public / private.

And now lets see how we manage to call and work with the custom class using namespaces.
Here is the main MXML code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="init()">
   
    <mx:Script>
    <![CDATA[

    import tip3.namespaces.*;
    import tip3.TestNamespaces;
   
    use namespace testing;
   
    private function init():void
    {
        TestNamespaces.alert();
    }
   
    ]]>
    </mx:Script>
   
</mx:Application>

First see the import tip3.namespaces.* that loads both namespaces. Now the important part is the use namespace testing;. This line assures that when the line TestNamespaces.alert(); is called, it will go into the testing static function alert():void function from TestNamespaces class, and you should get an Alert popup telling “Alert using test environment”.

If you replace the line use namespace testing; with use namespace release; you should get an Alert popup telling “Alert using release environment”, witch means the second alert() function from TestNamespace class was called.

Namespaces are good methods of keeping code inaccessible where you want so, like framework internal functions that shouldn’t be accessible from outside the framework. Best example is mx_internal used frequently in the Flex Framework itself.

You can download the source code from here.

  1. 2 Responses to “Flex Tip 3: Using Namespaces”

  2. really great tip!

    cheers!

    By Cristian on Jan 21, 2008

  3. easy and clear, thanks a lot

    By max on Feb 19, 2008

Post a Comment