本文翻译自:Passing arguments forward to another javascript function [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
I've tried the following with no success: 我已经尝试了以下方法,但均未成功:
function a(args){
b(arguments);
}
function b(args){
// arguments are lost?
}
a(1,2,3);
In function a, I can use the arguments keyword to access an array of arguments, in function b these are lost. 在函数a中,我可以使用arguments关键字来访问参数数组,而在函数b中,这些参数将丢失。 Is there a way of passing arguments to another javascript function like I try to do? 有没有办法像我尝试的那样将参数传递给另一个javascript函数?
#1楼
参考:https://stackoom.com/question/GQM1/将参数传递给另一个javascript函数-重复
#2楼
Spread operator 点差运算符
The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) are expected. 扩展运算符允许在需要多个参数(用于函数调用)或多个元素(用于数组文字)的位置扩展表达式。
ECMAScript ES6 added a new operator that lets you do this in a more practical way: ... Spread Operator . ECMAScript ES6添加了一个新的运算符,可让您以更实际的方式执行此操作 : ... Spread Operator 。
Example without using the apply
method: 不使用apply
方法的示例:
function a(...args){ b(...args); b(6, ...args, 8) // You can even add more elements } function b(){ console.log(arguments) } a(1, 2, 3)
Note This snippet returns a syntax error if your browser still uses ES5. 注意如果您的浏览器仍使用ES5,则此代码段将返回语法错误。
Editor's note: Since the snippet uses console.log()
, you must open your browser's JS console to see the result - there will be no in-page result. 编者注:由于该片段使用console.log()
,因此必须打开浏览器的JS控制台才能看到结果 -页面内将没有结果。
It will display this result: 它将显示以下结果:
In short, the spread operator can be used for different purposes if you're using arrays, so it can also be used for function arguments, you can see a similar example explained in the official docs: Rest parameters 简而言之,如果您使用数组,那么散布运算符可以用于不同的目的,因此它也可以用于函数参数,您可以看到官方文档中介绍的类似示例: Rest参数
#3楼
Use .apply()
to have the same access to arguments
in function b
, like this: 使用.apply()
可以对函数b
arguments
进行相同的访问,如下所示:
function a(){
b.apply(null, arguments);
}
function b(){
alert(arguments); //arguments[0] = 1, etc
}
a(1,2,3);
You can test it out here . 您可以在这里进行测试 。
#4楼
The explanation that none of the other answers supplies is that the original arguments are still available, but not in the original position in the arguments
object. 的解释,没有任何其他的答案用品的是,原来的参数仍然可用,但不是在原来的位置arguments
对象。
The arguments
object contains one element for each actual parameter provided to the function. arguments
对象为提供给函数的每个实际参数包含一个元素。 When you call a
you supply three arguments: the numbers 1
, 2
, and, 3
. 当你调用a
你提供三个参数:数字1
, 2
,和3
。 So, arguments
contains [1, 2, 3]
. 因此, arguments
包含[1, 2, 3]
。
function a(args){
console.log(arguments) // [1, 2, 3]
b(arguments);
}
When you call b
, however, you pass exactly one argument: a
's arguments
object. 但是,在调用b
时,您恰好传递了一个参数: a
的arguments
对象。 So arguments
contains [[1, 2, 3]]
(ie one element, which is a
's arguments
object, which has properties containing the original arguments to a
). 所以arguments
包含[[1, 2, 3]]
即,一个元件,这是a
的arguments
对象,它具有包含原始参数属性a
)。
function b(args){
// arguments are lost?
console.log(arguments) // [[1, 2, 3]]
}
a(1,2,3);
As @Nick demonstrated, you can use apply
to provide a set arguments
object in the call. 如@Nick所示,您可以使用apply
在调用中提供一个set arguments
对象。
The following achieves the same result: 以下实现相同的结果:
function a(args){
b(arguments[0], arguments[1], arguments[2]); // three arguments
}
But apply
is the correct solution in the general case. 但是在一般情况下, apply
是正确的解决方案。