7月
20.
Progression this の扱い(破) 無名関数, Func, functionの続き。
Progressionクラスを生成し、コンストラクタで”id”を指定します。
今回はSerialList.addCommand()内でのthisの扱いについて、全体のコードはこんな感じです。
全体の出力結果です。
基本は前回とほとんど同じなので省きます。
それで注目したいのが、10行目の
での出力が
でthis.parentの参照がsListになることです。
なので、19,20行目のthis.parentでsListにinsertCommandによる挿入を行わないと思ったとおりの実行をしてくれません。
“追加1”の実行されるタイミングはfunctionのCommandが終わってから。
“追加2″はおそらく、クラスに定義されているaddCommand(){}に登録されるんじゃないかと思われます。
【まとめ】
Progressionクラスを生成し、コンストラクタで”id”を指定します。
this.id = "yaimo"; trace("クラスのid: "+this.id); //出力:クラスのid: yaimo trace("普通にthis: "+this); //出力:[IndexScene sceneId="/index" id="yaimo" name="index" group="null"]
今回はSerialList.addCommand()内でのthisの扱いについて、全体のコードはこんな感じです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var sList:SerialList = new SerialList(); sList.id = "sList"; sList.addCommand( //[ new Trace("SerialList.addCommand内でのthis.id: " + this.id), new Func(function ():void { trace("SerialList.addCommand内でのthis" + this); trace("SerialList.addCommand内でのthis.parent" + this.parent); this.id = "Function"; trace("SerialList.addCommand内でのthis.id: " + this.id); }), function ():void { trace("SerialList.addCommand内でのthis.parent" + this.parent.id); this.parent.insertCommand(new Trace("追加1")); insertCommand(new Trace("追加2")); }, [//ここからParallelList new Func(function ():void { trace("SerialList.addCommand内でのthis: " + this); trace("SerialList.addCommand内でのthis.parent" + this.parent); }), ], |
全体の出力結果です。
SerialList.addCommand内でのthis.id: yaimo SerialList.addCommand(コード)での実験------new Func()--------- SerialList.addCommand内でのthis[Func id="null" name="command_47" group="null"] SerialList.addCommand内でのthis.parent[SerialList id="sList" name="command_42" group="null"] SerialList.addCommand内でのthis.id: Function ------------------------------------------------------------- SerialList.addCommand(コード)での実験-----function()---------- SerialList.addCommand内でのthis.parentsList ------------------------------------------------------------- 追加1 SerialList.ddCommand([コード])での実験-----new Func()---------- SerialList.addCommand内でのthis: [Func id="null" name="command_48" group="null"] SerialList.addCommand内でのthis.parent[ParallelList id="null" name="command_54" group="null"] -------------------------------------------------------------
基本は前回とほとんど同じなので省きます。
それで注目したいのが、10行目の
trace("SerialList.addCommand内でのthis.parent" + this.parent);
での出力が
SerialList.addCommand内でのthis.parent[SerialList id="sList" name="command_42" group="null"]
でthis.parentの参照がsListになることです。
なので、19,20行目のthis.parentでsListにinsertCommandによる挿入を行わないと思ったとおりの実行をしてくれません。
“追加1”の実行されるタイミングはfunctionのCommandが終わってから。
“追加2″はおそらく、クラスに定義されているaddCommand(){}に登録されるんじゃないかと思われます。
【まとめ】
- new Func(function:void{}),function():void{}内でのthisはFuncクラスを指す。
- SerialList.addCommand(){}の場合、function():void{this.parent}はSerialListを指す。
Tags: function, ParallelList, Progression, SerialList
