パッケージトップレベル
public final class Namespace
継承Namespace Inheritance Object

Namespace クラスには、名前空間を定義して操作するためのメソッドとプロパティが含まれています。名前空間を使用するシナリオには、次の 3 つがあります。

このクラスは、2 つの形式のコンストラクタメソッドを示しています。それぞれの形式が異なるパラメータを受け入れるためです。

XML、XMLList、および QName などのクラス同様、Namespace クラスには、ECMAScript for XML (E4X) 仕様 (ECMA-357 Edition 2) で定義されている強力な XML 処理規格が実装されています。

例の表示

関連項目

XML
XMLList
QName
ECMAScript for XML (E4X) specification (ECMA-357 edition 2)
XML 名前空間の使用


パブリックプロパティ
 プロパティ定義
 Inheritedconstructor : Object
指定されたオブジェクトインスタンスのクラスオブジェクトまたはコンストラクタ関数への参照です。
Object
  prefix : String
名前空間の接頭辞です。
Namespace
 Inheritedprototype : Object
[static] クラスまたは関数オブジェクトのプロトタイプオブジェクトへの参照です。
Object
  uri : String
名前空間の URI です。
Namespace
パブリック Methods
 メソッド定義
  
Namespace(uriValue:*)
Namespace オブジェクトを作成します。
Namespace
  
Namespace(prefixValue:*, uriValue:*)
prefixValue パラメータと uriValue パラメータの値に従って Namespace オブジェクトを作成します。
Namespace
 Inherited
指定されたプロパティがオブジェクトに定義されているかどうかを示します。
Object
 Inherited
Object クラスのインスタンスが、パラメータとして指定されたオブジェクトのプロトタイプチェーン内にあるかどうかを示します。
Object
 Inherited
指定されたプロパティが存在し、列挙できるかどうかを示します。
Object
 Inherited
ループ処理に対するダイナミックプロパティの可用性を設定します。
Object
  
Namespace.uri プロパティと同等です。
Namespace
  
指定されたオブジェクトの URI 値を返します。
Namespace
プロパティの詳細
prefixプロパティ
prefix:String  [read-write]

名前空間の接頭辞です。

実装
    public function get prefix():String
    public function set prefix(value:String):void
uriプロパティ 
uri:String  [read-write]

名前空間の URI です。

実装
    public function get uri():String
    public function set uri(value:String):void
コンストラクタの詳細
Namespace()コンストラクタ
public 関数 Namespace(uriValue:*)

Namespace オブジェクトを作成します。新しい Namespace オブジェクトの uri プロパティと prefix プロパティに割り当てられる値は、uriValue パラメータに渡される値の型によって決まります。

メモ : このクラスは、2 つのコンストラクタ項目を示しています。それぞれの形式が異なるパラメータを受け入れるためです。コンストラクタの動作は、各項目で詳細を示すように、渡されるパラメータの型と数によって異なります。ActionScript 3.0 はメソッドまたはコンストラクタのオーバーロードをサポートしていません。

パラメータ
uriValue:* — 名前空間の URI です。
Namespace()コンストラクタ 
public 関数 Namespace(prefixValue:*, uriValue:*)

prefixValue パラメータと uriValue パラメータの値に従って Namespace オブジェクトを作成します。このコンストラクタには両方のパラメータが必要です。

prefixValue パラメータの値は、次のように prefix プロパティに割り当てられます。

uriValue パラメータの値は、次のように uri プロパティに割り当てられます。

<strong>メモ :</strong> このクラスは、2 つのコンストラクタメソッド項目を示しています。それぞれの形式が異なるパラメータを受け入れるためです。コンストラクタの動作は、各項目で詳細を示すように、渡される引数の型と数によって異なります。ActionScript 3.0 はメソッドまたはコンストラクタのオーバーロードをサポートしていません。

パラメータ
prefixValue:* — 名前空間に使用する接頭辞です。
 
uriValue:* — 名前空間の URI です。
メソッドの詳細
toString()メソッド
AS3 function toString():String

Namespace.uri プロパティと同等です。

戻り値
String — ストリングとしての名前空間の URI です。
valueOf()メソッド 
AS3 function valueOf():String

指定されたオブジェクトの URI 値を返します。

戻り値
String — ストリングとしての名前空間の URI です。

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().
 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; } } }

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.
 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; } }

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).

 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); } }