this プロパティ

this

オブジェクトインスタンスまたはムービークリップインスタンスを参照します。スクリプトの実行時には、this はそのスクリプトを含むムービークリップインスタンスを参照します。メソッドの実行時には、this には呼び出されたメソッドを含むオブジェクトへの参照が入ります。

ボタンに割り当てられた on() イベントハンドラ内では、this はそのボタンを含むタイムラインを参照します。ムービークリップに割り当てられている onClipEvent() イベントハンドラ内では、this はそのムービークリップ自体のタイムラインを参照します。

this は、それが指定されたスクリプトのコンテキスト内で評価されます。したがって、クラスファイルで定義された変数については、スクリプト内で this を指定して参照することはできません。

対応バージョン : ActionScript 1.0、Flash Player 5

"ApplyThis.as" という名前の ActionsScript ファイルを作成し、次のコードを入力します。

class ApplyThis {
 var str:String = "Defined in ApplyThis.as";
 function conctStr(x:String):String {
 return x+x;
 }
 function addStr():String {
 return str;
 }
}

次に、FLA ファイルまたは別の ActionScript ファイル内に、次のコードを入力します。

var obj:ApplyThis = new ApplyThis();
var abj:ApplyThis = new ApplyThis();
abj.str = "defined in FLA or AS";
trace(obj.addStr.call(abj, null)); //output: defined in FLA or AS
trace(obj.addStr.call(this, null)); //output: undefined
trace(obj.addStr.call(obj, null)); //output: Defined in applyThis.as

同様に、ダイナミッククラスで定義された関数を呼び出すには、this を使用して適切なスコープで関数を呼び出す必要があります。

// incorrect version of Simple.as
/*
dynamic class Simple {
 function callfunc() {
 trace(func());
 }
}
*/
// correct version of Simple.as
dynamic class simple {
 function callfunc() {
 trace(this.func());
 }
}

FLA ファイルまたは別の ActionScript ファイル内に、次のコードを入力します。

var obj:Simple = new Simple();
obj.num = 0;
obj.func = function() {
 return true;
};
obj.callfunc();
// output: true

上記のコードは、this を callfunc() メソッドで使用した場合に機能します。ただし、不適切なバージョンの Simple.as を使用するとシンタックスエラーが発生し、上記の例ではコメントアウトされます。

次の例では、キーワード this は Circle オブジェクトを参照します。

function Circle(radius:Number):Void {
 this.radius = radius;
 this.area = Math.PI*Math.pow(radius, 2);
}
var myCircle = new Circle(4);
trace(myCircle.area);

ムービークリップ内のフレームに割り当てられた次のステートメントでは、キーワード this は現在のムービークリップを参照します。

// sets the alpha property of the current movie clip to 20
this._alpha = 20;

MovieClip.onPress ハンドラ内の次のステートメントでは、キーワード this は現在のムービークリップを参照します。

this.square_mc.onPress = function() {
 startDrag(this);
};
this.square_mc.onRelease = function() {
 stopDrag();
};

関連項目

on ハンドラ, onClipEvent ハンドラ


 

このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート

現在のページ: http://livedocs.adobe.com/flash/9.0_jp/main/00001246.html