apply (Function.apply メソッド)

public apply(thisObject: Object , [argArray: Array ]) : Void

ActionScript が呼び出す関数内で使用される thisObject の値を指定します。このメソッドは、呼び出される関数に渡されるパラメータも指定します。apply() は Function クラスのメソッドなので、ActionScript 内のすべての Function オブジェクトのメソッドとしても使用できます。

パラメータは、カンマ区切りリストとしてパラメータを指定する Function.call() とは異なり、Array オブジェクトとして指定します。これは、スクリプトが実際に実行されるまで、渡されるパラメータ数が不明である場合にも便利です。

呼び出された関数が戻り値として指定する値を返します。

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

パラメータ

thisObject:Object - myFunction の適用先のオブジェクト。

argArray:Array (オプション) - エレメントをパラメータとして myFunction に渡す配列。

次の 2 つの関数呼び出しは同じです。

Math.atan2(1, 0)
Math.atan2.apply(null, [1, 0])

次の簡単な例は、apply() を使用してパラメータの配列を渡す方法を示しています。

function theFunction() {
    trace(arguments);
}

// create a new array to pass as a parameter to apply()
var firstArray:Array = new Array(1,2,3);
theFunction.apply(null,firstArray);
// outputs: 1,2,3

// create a second array to pass as a parameter to apply()
var secondArray:Array = new Array("a", "b", "c");
theFunction.apply(null,secondArray);
// outputs a,b,c

次の例では、apply() を使用してパラメータの配列を渡し、this の値を指定します。

// define a function 
function theFunction() {
    trace("this == myObj? " + (this == myObj));
    trace("arguments: " + arguments);
}

// instantiate an object
var myObj:Object = new Object();

// create arrays to pass as a parameter to apply()
var firstArray:Array = new Array(1,2,3);
var secondArray:Array = new Array("a", "b", "c");

// use apply() to set the value of this to be myObj and send firstArray
theFunction.apply(myObj,firstArray);
// output: 
// this == myObj? true
// arguments: 1,2,3

// use apply() to set the value of this to be myObj and send secondArray
theFunction.apply(myObj,secondArray);
// output: 
// this == myObj? true
// arguments: a,b,c

関連項目

call (Function.call メソッド)


 

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

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