ホーム > AS3, Command, Progression > Progression this の扱い(破) 無名関数, Func, function

Progression this の扱い(破) 無名関数, Func, function

2009 年 7 月 19 日 コメントをどうぞ コメント
Progression this の扱い(序) 無名関数, Func, functionの続き。

Progressionクラスを生成し、コンストラクタで”id”を指定します。

this.id = "yaimo";
trace("クラスのid: "+this.id);
//出力:クラスのid: yaimo
trace("普通にthis: "+this);
//出力:[IndexScene sceneId="/index" id="yaimo" name="index" group="null"]


今回はaddCommand()内でのthisの扱いについて、全体のコードはこんな感じです。

addCommand(
				new Trace("addCommand内でのthis.id: " + this.id),
				new Func(function ():void
				{
					trace("addCommand内でのthis" + this);
					trace("addCommand内でのthis.parent" + this.parent);
					this.id = "Function";
					trace("addCommand内でのthis.id: " + this.id);
					trace("addCommand内でのthis.parent.id: " + this.parent.id);
					this.parent.id = "parentはSerialList";
					trace("addCommand内でのthis.parent" + this.parent);
				}),
				function ():void
				{
					trace("addCommand内でのthis" + this);
					trace("addCommand内でのthis.parent" + this.parent);
					this.id = "fuction";
					trace("addCommand内でのthis.id: " + this.id);
					trace("addCommand内でのthis.parent.id: "+this.parent.id);
				},
				[//ここからParallelList
				new Func(function ():void
				{
					trace("addCommand内でのthis" + this);
					trace("addCommand内でのthis.parent" + this.parent);
					this.id = "Function";
					trace("addCommand内でのthis.id: "+this.id);
					trace("addCommand内でのthis.parent.id: "+this.parent.id);
				}),
				]
			);


全体の出力結果です。

addCommand(コード)での実験-----new Func()----------
addCommand内でのthis[Func id="null" name="command_12" group="null"]
addCommand内でのthis.parent[SerialList id="null" name="command_4" group="null"]
addCommand内でのthis.id: Function
addCommand内でのthis.parent.id: null
addCommand内でのthis.parent[SerialList id="parentはSerialList" name="command_4" group="null"]
 
addCommand([コード])での実験-----function()----------
addCommand内でのthis[Func id="null" name="command_28" group="null"]
addCommand内でのthis.parent[SerialList id="null" name="command_4" group="null"]
addCommand内でのthis.id: fuction
addCommand内でのthis.parent.id: null
 
addCommand([コード])での実験-----new Func()----------
addCommand内でのthis[Func id="null" name="command_19" group="null"]
addCommand内でのthis.parent[ParallelList id="null" name="command_29" group="null"]
addCommand内でのthis.id: Function
addCommand内でのthis.parent.id: null




実際にコードを細かく見ていきます。

まず2行目の、

new Trace("addCommand内でのthis.id: " + this.id)


これは普通にクラス変数なので”yaimo”の出力です。

addCommand内でのthis.id: yaimo


5~11行目

trace("addCommand内でのthis" + this);
trace("addCommand内でのthis.parent" + this.parent);
this.id = "Function";
trace("addCommand内でのthis.id: " + this.id);
trace("addCommand内でのthis.parent.id: " + this.parent.id);
this.parent.id = "parentはSerialList";
trace("addCommand内でのthis.parent" + this.parent);

出力は

1.addCommand内でのthis[Func id="null" name="command_12" group="null"]
2.addCommand内でのthis.parent[SerialList id="null" name="command_4" group="null"]
3.addCommand内でのthis.id: Function
4.addCommand内でのthis.parent.id: null
5.addCommand内でのthis.parent[SerialList id="parentはSerialList" name="command_4" group="null"]


1.thisはFuncコマンドを指すようです。
2.Funcコマンドの親はクラス全体のことを指すと思ったのですが、
どうやらaddCommandで登録したSerialListの方を指すようです。
3.1より、this = Funcなのでidを登録して出力しました。
4.2からわかるようにSerialListのidは”null”です。
5.なので同様に、this.parent.idを登録して出力しました。




■じゃぁ、function():void{}でやってみたらどうなるの?っていうのが15~19行目。
同じですね。function():void{}はnew Func(){function():void{}}を省略した書き方だった気がします。

■3つ目は、addCommand()内に”[]“を使用することによってParallelListを使ったらどうなるかという実験です。
this.parentがParallelListを参照するようになりました。
なるほど、insertで追加するタイミングではParallelListにもなるということですね。
これは応用できそうです。 

【まとめ】
  • new Func(function:void{}),function():void{}内でのthisはFuncクラスを指す

  • this.parentがSerialListなどを指してるのが意外。
    スーパークラスでaddCommand()を定義してあるだろうSerialListを指すのはあたりまえか(?)
    insertCommandをfunction内で記述する際は、クラスに定義されているaddCommand(){}に登録に追加するというイメージ

    addCommand(
    function():void{
    insertCommand(new Trace("this.addCommand()に追加"));
    });


これが次につながってくる。つづく。

Bookmark and Share

関連する投稿

  1. コメントはまだありません。
  1. トラックバックはまだありません。