如何将参数数组传递给 JavaScript 中的另一个函数? [英] How to pass arguments array to another function in JavaScript?

查看:21
本文介绍了如何将参数数组传递给 JavaScript 中的另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JavaScript 函数:

I have a function in JavaScript:

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

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

if I call it with test(1,3,5), it prints out 3 because there are 3 arguments. How do I call test from within another function and pass the other function's arguments?

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

我想调用 other 并让它调用 test 及其 arguments 数组.

I want to call other and have it call test with its arguments array.

推荐答案

看一看apply():

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

function other(){
    test.apply(null, arguments);
}

这篇关于如何将参数数组传递给 JavaScript 中的另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆