Javascript 如何将所有参数作为集合传递给另一个函数而不是作为单个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4706236/
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
How to pass all arguments as collection to another function and not as single argument?
提问by Hansjoerg
I have a function that accepts any number and kind of arguments, so no specific parameter has been defined. This function should call another function passing all arguments.
我有一个函数可以接受任意数量和类型的参数,所以没有定义特定的参数。这个函数应该调用另一个传递所有参数的函数。
The problem is that I can pass "arguments" in order to include all arguments but in this case it will work like a single argument and not the way we expect arguments to work.
问题是我可以传递“参数”以包含所有参数,但在这种情况下,它将像单个参数一样工作,而不是我们期望参数工作的方式。
An example:
一个例子:
The main function:
主要功能:
function handleCall() {
// let's call a sub-function
// and pass all arguments (my question is how this is handled the right way)
function callSubFunction( arguments );
}
function callSubfunction( userid, customerid, param) {
// passed arguments are now
alert( 'userid = ' + userid );
// this will not work, you have to use arguments[2]
alert( param );
}
The example call:
handleCall( 1029, 232, 'param01' );
Using the approach above, all arguments will be stored in "userid" as pseudo-array and items can be accessed e.g. arguments[2] but not using the parameter name "param".
使用上述方法,所有参数都将作为伪数组存储在“userid”中,并且可以访问项目,例如参数 [2] 但不使用参数名称“param”。
In ColdFusion, the solution for such stuff is the parameter "argumentCollection", this way you can pass parameters stored in a structure without being converted to a single argument with the type struct containing all key/values.
在 ColdFusion 中,解决此类问题的方法是参数“argumentCollection”,这样您就可以传递存储在结构中的参数,而无需将其转换为包含所有键/值的结构类型的单个参数。
How can I achieve the same with JavaScript?
我怎样才能用 JavaScript 达到同样的效果?
回答by alexmngn
If you want to do the same with the spread syntax, you can use the following:
如果你想对传播语法做同样的事情,你可以使用以下内容:
function handleCall(...args) {
callSubFunction(...args);
}
回答by user113716
You can use the .apply()
methodto call a function and pass the arguments as a set.
您可以使用该.apply()
方法调用函数并将参数作为集合传递。
callSubFunction.apply( this, arguments );
The first argument will set the value of this
in the allSubFunction
method. I just set it to the current this
value. The second is the collection of arguments to send.
第一个参数将设置的值this
在allSubFunction
方法。我只是将其设置为当前this
值。第二个是要发送的参数集合。
So your handleCall()
function will look like:
所以你的handleCall()
函数看起来像:
function handleCall() {
//set the value of "this" and pass on the arguments object
callSubFunction.apply( this, arguments );
}
It isn't required that you send an Arguments
object. You could send an Array
of arguments if the circumstance required.
您不需要发送Arguments
对象。Array
如果情况需要,您可以发送一个参数。
回答by Jimmy Chandra
Use apply like so:
像这样使用 apply:
function Foo()
{
Bar.apply(this, arguments);
}
function Bar(a, b)
{
alert(a);
alert(b);
}