将参数传递给另一个javascript函数 [英] Passing arguments forward to another javascript function

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

问题描述

我尝试过以下操作但没有成功:

I've tried the following with no success:

function a(args){
    b(arguments);
}

function b(args){
    // arguments are lost?
}

a(1,2,3);

在函数a中,我可以使用arguments关键字来访问参数数组,在函数b中这些迷路了。有没有办法将参数传递给另一个javascript函数,就像我尝试做的那样?

In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. Is there a way of passing arguments to another javascript function like I try to do?

推荐答案

使用 .apply() 在函数 b 中对参数的相同访问权限,如下所示:

Use .apply() to have the same access to arguments in function b, like this:

function a(){
    b.apply(null, arguments);
}
function b(){
   alert(arguments); //arguments[0] = 1, etc
}
a(1,2,3);​

您可以在这里进行测试

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

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