4

私は JavaScript で関数を持っています:

function test() {
  console.log(arguments.length);
}

で呼び出すと、引数が 3 つあるためtest(1,3,5)出力されます。3別の関数内から test を呼び出して、別の関数の引数を渡すにはどうすればよいですか?

function other() {
  test(arguments); // always prints 1
  test(); // always prints 0
}

私はother呼び出しtestて、そのarguments配列で呼び出したいと思っています。

4

2 に答える 2

7

見てみましょうapply()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

function other(){
    test.apply(null, arguments);
}
于 2013-11-09T02:09:45.430 に答える
0

このように引数を渡してみてはいかがでしょうか。

function other() {
  var testing=new Array('hello','world');
  test(testing); 
}
function test(example) {
  console.log(example[0] + " " + example[1]);
}

出力:hello world

動作中のJSFiddleは次のとおりです。

于 2013-11-09T02:19:34.220 に答える