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

2024-04-11

我在 JavaScript 中有一个函数:

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

如果我用它来称呼它test(1,3,5),它打印出3因为有3个参数。如何从另一个函数中调用 test 并传递另一个函数的参数?

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

我想打电话other并让它调用test以其arguments array.


看一眼apply():

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

function other(){
    test.apply(null, arguments);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将参数数组传递给 JavaScript 中的另一个函数? [复制] 的相关文章

随机推荐