jequery の ajax complete: のサンプル等では、
function(XMLHttpRequest, textStatus)
の様に記載がありますが、実際に次のようにテストすると、
complete:function(XMLHttpRequest, textStatus){
console.log("complete arguments.length(" + arguments.length + ")");
}
arguments.length が3を指しており、引数が3つあることが分かります。
このような時に、
complete:function(XMLHttpRequest, textStatus, arg3){
funcA(XMLHttpRequest, textStatus, arg3);
}
の様に引数を固定で書くのではなく、
arguments.length を元に、 その引数をそのまま使用して、funcA を 呼び出すような仕組みは作れないものでしょうか?
arguments.length==3 の時は、下記の様に呼び出して。
funcA(arguments[0],arguments[1],arguments[2]);
arguments.length==4 の時は、下記の様に呼び出して。
funcA(arguments[0],arguments[1],arguments[2],arguments[3]);
の様に呼び出す。
どうかよろしくお願い申し上げます。
Function オブジェクトの apply メソッドを使います。
こんな感じ。
function x1(a) { console.log("x1: " + a); } function x2(a, b) { console.log("x2: " + a + ", " + b); } function x3(a, b, c) { console.log("x3: " + a + ", " + b + ", " + c); } function x4(a, b, c, d) { console.log("x4: " + a + ", " + b + ", " + c + ", " + d); } function test() { var arg = [1, 2, 3]; x1.apply(null, arg); x2.apply(null, arg); x3.apply(null, arg); x4.apply(null, arg); } test();
結果。
x1: 1 x2: 1, 2 x3: 1, 2, 3 x4: 1, 2, 3, undefined
jsFiddle で試してみたものがこちらです。
http://jsfiddle.net/a_kuma3/dHAw8/embedded/result/
funcA.call(null, arguments)
とします。
funcA(arguments...)
という書き方ができたりします。
# funcA.apply(null, arguments); というjavascriptに変換されます