如何将参数数组传递给JavaScript中的另一个函数?

at.*_*at. 4 javascript arguments function

我有一个JavaScript函数:

function test() {
  console.log(arguments.length);
}
Run Code Online (Sandbox Code Playgroud)

如果我打电话给它test(1,3,5),它打印出来,3因为有3个参数.如何从另一个函数中调用test并传递其他函数的参数?

function other() {
  test(arguments); // always prints 1
  test(); // always prints 0
}
Run Code Online (Sandbox Code Playgroud)

我想调用othertest并使用它的arguments数组调用它.

Ale*_*euk 7

看看apply():

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

function other(){
    test.apply(null, arguments);
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为你的意思是`test.apply(this,arguments);` (4认同)