Top Level

# Namespace - AS3

Package | Top Level
Class | public final class Namespace
Inheritance | Namespace Object

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The Namespace class contains methods and properties for defining and working with namespaces. There are three scenarios for using namespaces:

- Namespaces of XML objects Namespaces associate a namespace prefix with a Uniform Resource Identifier (URI) that identifies the namespace. The prefix is a string used to reference the namespace within an XML object. If the prefix is undefined, when the XML is converted to a string, a prefix is automatically generated.
- Namespace to differentiate methods Namespaces can differentiate methods with the same name to perform different tasks. If two methods have the same name but separate namespaces, they can perform different tasks.
- Namespaces for access control Namespaces can be used to control access to a group of properties and methods in a class. If you place the properties and methods into a private namespace, they are inaccessible to any code that does not have access to that namespace. You can grant access to the group of properties and methods by passing the namespace to other classes, methods or functions.

This class shows two forms of the constructor method because each form accepts different parameters.

This class (along with the XML, XMLList, and QName classes) implements powerful XML-handling standards defined in ECMAScript for XML (E4X) specification (ECMA-357 edition 2).

View the examples

More examples

Using XML namespaces

Learn more

ECMAScript for XML (E4X) specification (ECMA-357 edition 2)

Related API Elements

XML
XMLList
QName

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | prefix : String The prefix of the namespace. | Namespace
|  | uri : String The Uniform Resource Identifier (URI) of the namespace. | Namespace

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Namespace(prefixValue:*, uriValue:*) Creates a Namespace object according to the values of the prefixValue and uriValue parameters. | Namespace
|  | Namespace(uriValue:*) Creates a Namespace object. | Namespace
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Equivalent to the Namespace.uri property. | Namespace
|  | valueOf():String Returns the URI value of the specified object. | Namespace

Property Detail

### prefix

property

`prefix:String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

The prefix of the namespace.

Implementation
`public function get prefix():String`
`public function set prefix(value:String):void`

### uri

property

`uri:String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

The Uniform Resource Identifier (URI) of the namespace.

Implementation
`public function get uri():String`
`public function set uri(value:String):void`
Constructor Detail

### Namespace

()
Constructor

`public function Namespace(prefixValue:*, uriValue:*)`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Creates a Namespace object according to the values of the `prefixValue` and `uriValue` parameters. This constructor requires both parameters.

The value of the `prefixValue` parameter is assigned to the `prefix` property as follows:

- If `undefined` is passed, `prefix` is set to `undefined`.
- If the value is a valid XML name, as determined by the `isXMLName()` function, it is converted to a string and assigned to the `prefix` property.
- If the value is not a valid XML name, the `prefix` property is set to `undefined`.

The value of the `uriValue` parameter is assigned to the `uri` property as follows:

- If a QName object is passed, the `uri` property is set to the value of the QName object's `uri` property.
- Otherwise, the `uriValue` parameter is converted to a string and assigned to the `uri` property.

Note: This class shows two constructor method entries because each form accepts different parameters. The constructor behaves differently depending on the type and number of arguments passed, as detailed in each entry. ActionScript 3.0 does not support method or constructor overloading.

Parameters

| `prefixValue:*` — The prefix to use for the namespace.
| `uriValue:*` — The Uniform Resource Identifier (URI) of the namespace.

### Namespace

()
Constructor

`public function Namespace(uriValue:*)`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Creates a Namespace object. The values assigned to the `uri` and `prefix` properties of the new Namespace object depend on the type of value passed for the `uriValue` parameter:

- If no value is passed, the `prefix` and `uri` properties are set to an empty string.
- If the value is a Namespace object, a copy of the object is created.
- If the value is a QName object, the `uri` property is set to the `uri` property of the QName object.

Note: This class shows two constructor entries because each form accepts different parameters. The constructor behaves differently depending on the type and number of parameters passed, as detailed in each entry. ActionScript 3.0 does not support method or constructor overloading.

Parameters

| `uriValue:*` — The Uniform Resource Identifier (URI) of the namespace.

Method Detail

### toString

()
method

`AS3 function toString():String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Equivalent to the `Namespace.uri` property.

Returns

| `String` — The Uniform Resource Identifier (URI) of the namespace, as a string.

### valueOf

()
method

`AS3 function valueOf():String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9

Returns the URI value of the specified object.

Returns

| `String` — The Uniform Resource Identifier (URI) of the namespace, as a string.

Examples How to use this example
NamespaceExample.as
The following example shows how to work with namespaces defined in XML objects. This is accomplished with the following steps:

1. The example defines three Namespace objects, each with a unique URI that defines a namespace.
2. The example defines an XML variable named `myXML` and assigns it to the return value of `getRSS()`. The `getRSS()` method defines an XML object that contains several namespaces and returns that XML object.
3. The example declares and evaluates an Array variable by calling the `parseRSS()` method with `myXML` passed to it. In `parseRSS()`, the default XML namespace is defined as `rss` and the example defines an XMLList variable by assigning the list of `item` objects in `myXML`. An array is created and populated with various nodes within `myXML.item`. The array is then returned.
4. The elements in the array are printed using a `for` loop and three calls to `trace()`.

Copy

    package {
        import flash.display.Sprite;

        public class NamespaceExample extends Sprite {
            private var rss:Namespace = new Namespace("http://purl.org/rss/1.0/");
            private var rdf:Namespace = new Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            private var dc:Namespace  = new Namespace("http://purl.org/dc/elements/1.1/");

            public function NamespaceExample() {
                var myXML:XML = getRSS();
                var rssItems:Array = parseRSS(myXML);

                var len:uint = rssItems.length;
                for (var i:uint; i < len; i++) {
                    trace(rssItems[i].title);
                    trace(rssItems[i].creator);
                    trace(rssItems[i].date);
                    // Adobe Flash Developer Center
                    // Adobe
                    // 2005-08-08
                    // Flex Developer Center
                    // Adobe
                    // 2005-10-16
                }
            }

            private function parseRSS(rssXML:XML):Array {
                default xml namespace = rss;

                var items:XMLList = rssXML.item;

                var arr:Array = new Array();
                var len:uint = items.length();
                for (var i:uint; i < len; i++) {
                    arr.push({title:items[i].title, creator:items[i].dc::creator, date:items[i].dc::date});
                }

                return arr;
            }

            private function getRSS():XML {
                var myXML:XML =  <rdf:RDF
                  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                  xmlns="http://purl.org/rss/1.0/"
                  xmlns:dc="http://purl.org/dc/elements/1.1/"
                >
                  <channel rdf:about="http://www.xml.com/cs/xml/query/q/19">
                    <title>Test RSS</title>
                    <link>http://www.adobe.com/</link>
                    <description>This is a test RSS document.</description>
                    <language>en-us</language>
                    <items>
                      <rdf:Seq>
                    <rdf:li rdf:resource="http://www.adobe.com/devnet/flash/"/>
                    <rdf:li rdf:resource="http://www.adobe.com/devnet/flex/"/>
                      </rdf:Seq>
                    </items>
                  </channel>
                  <item rdf:about="http://www.adobe.com/devnet/flash/">
                    <title>Adobe Flash Developer Center</title>
                    <link>http://www.adobe.com/devnet/flash/</link>
                    <description>Welcome to the Flash Developer Center</description>
                    <dc:creator>Adobe</dc:creator>
                    <dc:date>2005-08-08</dc:date>
                  </item>
                  <item rdf:about="http://www.adobe.com/devnet/flex/">
                    <title>Flex Developer Center</title>
                    <link>http://www.adobe.com/devnet/flex/</link>
                    <description>Welcome to the Flex Developer Center</description>
                    <dc:creator>Adobe</dc:creator>
                    <dc:date>2005-10-16</dc:date>
                  </item>
                </rdf:RDF>;

                return myXML;
            }
        }
    }

Namespace_2_Example.as
The following example shows how namespaces can be used to differentiate methods that have the same name but perform different tasks. In this example, three methods named `hello()` reside in separate namespaces; each returns a different string when called.
Copy

    package {

        import flash.display.Sprite;

        public class Namespace_2_Example extends Sprite {
            public function Namespace_2_Example() {
                var vocab:MultilingualVocabulary = new MultilingualVocabulary();

                trace(vocab.hello());    // hello

                var languages:Array = vocab.getLanguages();

                for (var i:uint; i < languages.length; i++) {
                    var ns:Namespace = languages[i];
                    if (ns != null) {
                        trace(ns.toString() + ": " + vocab.ns::hello());
                        // hello
                        // MultilingualVocabulary:Hawaiian: aloha
                        // MultilingualVocabulary:French: bon jour
                    }
                }
            }
        }
    }

    class MultilingualVocabulary {
        public namespace French;
        public namespace Hawaiian;
        private var languages:Array;

        public function MultilingualVocabulary() {
            languages = new Array(Hawaiian, French);
        }

        public function hello():String {
            return "hello";
        }

        Hawaiian function hello():String {
            return "aloha";
        }

        French function hello():String {
            return "bon jour";
        }

        public function getLanguages():Array {
            return languages;
        }
    }

Namespace_3_Example.as
The following example uses namespace names to select an appropriate variable value. It shows how you can store a namespace value in a variable and use that variable to refer to objects within that namespace.

The example defines namespaces and colors that correspond to mouse states for a rectangular button. Each time the button is drawn, the example applies the appropriate color (out is red; over is yellow; down is white) by referencing the `bgcolor` variable for the corresponding namespace (`out`, `over`, `down`).

Copy

    package {
        import flash.display.Sprite;

        public class Namespace_3_Example extends Sprite {
            public function Namespace_3_Example() {
                addChild(new StateButton("Press Me."));
            }
        }
    }

    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.events.MouseEvent;

    class StateButton extends Sprite{
        private namespace out;
        private namespace over;
        private namespace down;
        private var label:TextField;
        private var labelTxt:String;
        private var ns:Namespace;
        out var bgColor:Number = 0xFF0000;
        over var bgColor:Number = 0xFFFF00;
        down var bgColor:Number = 0xFFFFFF;

        public function StateButton(str:String) {
            buttonMode = true;
            labelTxt = str;
            ns = out;
            draw();
            addLabel();
            addListeners();
        }

        private function addLabel():void {
            label = new TextField();
            label.text = labelTxt;
            label.width = 50;
            label.height = 20;
            label.mouseEnabled = false;
            addChild(label);
        }

        private function addListeners():void {
            addEventListener(MouseEvent.MOUSE_UP, mouseOverHandler);
            addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
            addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        }

        private function mouseOutHandler(e:Event):void {
            ns = out;
            draw();
        }

        private function mouseOverHandler(e:Event):void {
            ns = over;
            draw();
        }

        private function mouseDownHandler(e:Event):void {
            ns = down;
            draw();
        }

        private function draw():void {
            this.graphics.clear();
            this.graphics.beginFill(ns::bgColor);
            this.graphics.drawRect(0, 0, 60, 20);
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00