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

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

问题描述

我有一个JavaScript函数:

I have a function in JavaScript:

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

如果我把它叫做测试(1,3,5),它打印出 3 因为有3个参数。如何调用测试从另一个函数内,并通过其他函数的参数?

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
}

我要打电话,并将它称之为测试参数阵列。

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

推荐答案

看看适用()

<一个href=\"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);
}

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

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