将参数传递给另一个 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3914557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing arguments forward to another javascript function
提问by Anders Nygaard
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. Is there a way of passing arguments to another javascript function like I try to do?
在函数 a 中,我可以使用 arguments 关键字来访问参数数组,在函数 b 中这些都丢失了。有没有办法像我尝试做的那样将参数传递给另一个 javascript 函数?
回答by Nick Craver
回答by Walter Chapilliquen - wZVanG
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)
NoteThis snippet returns a syntax error if your browser still uses ES5.
Note如果您的浏览器仍然使用 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 noin-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 parameters
回答by Wayne
The explanation that none of the other answers supplies is that the original arguments arestill 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. When you call a
you supply three arguments: the numbers 1
, 2
, and, 3
. So, arguments
contains [1, 2, 3]
.
arguments
对于提供给函数的每个实际参数,该对象包含一个元素。当您打电话时,您需要a
提供三个参数:数字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 oneargument: a
's arguments
object. So arguments
contains [[1, 2, 3]]
(i.e. one element, which is a
's arguments
object, which has properties containing the original arguments to a
).
b
但是,当您调用时,您只传递了一个参数:a
的arguments
对象。所以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
提供一个集合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
在一般情况下是正确的解决方案。