Javascript:函数的“无限”参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9338291/
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
Javascript: "Infinite" parameters for function?
提问by auroranil
In Chrome, when I type console.log
in the one below:
在 Chrome 中,当我输入console.log
以下内容时:
console.log("A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter");
...it prints it correctly, with no errors or warnings. I appended more parameters, but it still prints it out correctly.
...它打印正确,没有错误或警告。我附加了更多参数,但它仍然正确打印出来。
console.log("A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter", "A parameter");
How can I have an "infinite" parameter function just like that one?
我怎么能有一个像那个一样的“无限”参数函数?
回答by hugomg
Functions can access an array-like object called arguments
that contains all the arguments that they received
函数可以访问一个类似数组的对象arguments
,该对象包含它们收到的所有参数
function print_my_arguments(/**/){
var args = arguments;
for(var i=0; i<args.length; i++){
console.log(args[i]);
}
};
And you can do the opposite conversion (call a function given a list of arguments) with the apply method:
您可以使用 apply 方法进行相反的转换(调用给定参数列表的函数):
// These are equivalent:
print_my_arguments(1,2,3);
print_my_arguments.apply(null, [1,2,3]);
// The first parameter to `apply` is the `this`.
// It is used when the function is a method.
foo.bar(1,2,3);
var f = foo.bar; f.apply(foo, [1,2,3]);
Some important points to note:
需要注意的一些要点:
arguments
isn't an actual array and it has none of the usual array methods (slice, join, etc). You can convert it to an array with the following line:var args = Array.prototype.slice.call(arguments);
Slice is also useful if you want your array to only contain the non-named arguments that were received:
function foo(first_arg, second_arg /**/){ var variadic_args = Array.prototype.slice.call(arguments, 2); }
Not every browser can handle an arbitrarily large number of function parameters. Last time I tested this, in Chrome and IE there was a stackoverflow after some 200.000 arguments. If your function can receive an arbitrarily large number of arguments, consider packing all of those arguments in an regular array instead.
Those
/**/
comments that appear in the arguments lists for my examples are not mandatory. They are just a coding a convention that I use to mark my variadic functions and differentiate them from regular functions.// A quick glance would suggest that this function receives no // parameters but actually it is a variadic function that gets // its parameters via the `arguments` object. function foo(){ console.log(arguments.length); }
arguments
不是一个实际的数组,它没有任何常用的数组方法(切片、连接等)。您可以使用以下行将其转换为数组:var args = Array.prototype.slice.call(arguments);
如果您希望数组只包含接收到的非命名参数,切片也很有用:
function foo(first_arg, second_arg /**/){ var variadic_args = Array.prototype.slice.call(arguments, 2); }
并非每个浏览器都可以处理任意数量的函数参数。上次我对此进行测试时,在 Chrome 和 IE 中,经过大约 200.000 个参数后出现了 stackoverflow。如果您的函数可以接收任意数量的参数,请考虑将所有这些参数打包到一个常规数组中。
/**/
出现在我的示例的参数列表中的那些注释不是强制性的。它们只是我用来标记可变参数函数并将它们与常规函数区分开来的编码约定。// A quick glance would suggest that this function receives no // parameters but actually it is a variadic function that gets // its parameters via the `arguments` object. function foo(){ console.log(arguments.length); }
回答by Felix Edelmann
The modern way of doing this is using rest parameters:
这样做的现代方法是使用rest 参数:
function printArguments(...args) {
args.forEach((arg, index) => {
console.log(`Argument ${index}:`, arg);
});
}
printArguments('hello', true, new Date());
By using the ...args
syntax, all parameters are saved in an array named args
.
通过使用该...args
语法,所有参数都保存在名为 的数组中args
。
Except of Internet Explorer, all browsers already ship this feature in their newest version.
除 Internet Explorer 外,所有浏览器都已在其最新版本中提供此功能。
Fiddle: https://jsfiddle.net/Lbf0stst/
回答by DynasteN
You can use the arguments array: jsfiddle.net/kUnJ2/
您可以使用参数数组:jsfiddle.net/kUnJ2/
function foo() {
for (var i = 0; i < arguments.length; i++) {
document.body.innerHTML += arguments[i];
}
}
foo("There ", "are ", "as ", "much ", "arguments ", "as ", "you ", "want.");