JavaScript 函数中的无限参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6396046/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:32:23  来源:igfitidea点击:

Unlimited arguments in a JavaScript function

javascriptarraysfunction

提问by rhavd

Can a JavaScript function take unlimited arguments? Something like this:

JavaScript 函数可以接受无限参数吗?像这样的东西:

testArray(1, 2, 3, 4, 5...);

I am trying:

我在尝试:

var arr = [];
function testArray(A) {
    arr.push(A);
}

But this doesn't work (output is only the first argument). Or the only way is:

但这不起作用(输出只是第一个参数)。或者唯一的方法是:

function testArray(a, b, c, d, e...) {

}

Thanks

谢谢

回答by Pointy

There's a weird "magic" variable you can reference called "arguments":

您可以引用一个奇怪的“魔法”变量,称为“参数”:

function manyArgs() {
  for (var i = 0; i < arguments.length; ++i)
    alert(arguments[i]);
}

It's likean array, but it's not an array. In fact it's so weird that you really shouldn't use it much at all. A common practice is to get the values of it into a realarray:

就像一个数组,但它不是一个数组。事实上,它太奇怪了,你根本不应该使用它。一种常见的做法是将它的值放入一个真正的数组中:

function foo() {
  var args = Array.prototype.slice.call(arguments, 0);
  // ...

In that example, "args" would be a normal array, without any of the weirdness. There are all sorts of nasty problems with "arguments", and in ECMAScript 5 its functionality will be curtailed.

在那个例子中,“args”将是一个普通的数组,没有任何奇怪的地方。“参数”存在各种令人讨厌的问题,在 ECMAScript 5 中,它的功能将被缩减。

edit— though using the .slice()function sure is convenient, it turns out that passing the argumentsobject out of a function causes headaches for optimization, so much so that functions that do it may not get optimized at all. The simple, straightforward way to turn argumentsinto an array is therefore

编辑——虽然使用该.slice()函数确实很方便,但事实证明,将arguments对象传递出函数会导致优化的麻烦,以至于执行它的函数可能根本不会得到优化。因此,arguments转换为数组的简单直接的方法是

function foo() {
  var args = [];
  for (var i = 0; i < arguments.length; ++i) args[i] = arguments[i];
  // ...
}

More about argumentsand optimization.

更多关于arguments和优化。

回答by Lucien Greathouse

As of ECMAScript 2015 (or ES6) we also have access to rest parametersthat give us a slightly cleaner way to manage arguments:

从 ECMAScript 2015(或 ES6)开始,我们还可以访问其余参数,这为我们提供了一种更简洁的方法来管理参数:

function foo(a, b, ...others) {
    console.log("a and b are ", a, b);

    for (let val of others) {
        console.log(val);
    }
}

foo(1, 2, 3, 4, 5);

At the time of this writing, this is supported by Chrome 47+, Firefox 15+, and Edge. The feature is also available via both Babeland TypeScripttranspiling down to ES5.

在撰写本文时,Chrome 47+、Firefox 15+ 和 Edge 支持此功能。该功能还可以通过BabelTypeScript转换为 ES5 来使用。

回答by Marius Tancredi

With ECMAScript 6, you can use rest of arguments syntax:

使用 ECMAScript 6,您可以使用其余参数语法:

const testArray = (...args) => {
    console.log(args);
};

testArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

回答by Nosaj Neirbo

You can also just "cast" it, saving you the ugly loop:

您也可以“投射”它,从而节省丑陋的循环:

var getArguments = function() {
    return arguments;
};

var foo = getArguments(1,2,3,4);

// console.log(foo.slice()); => TypeError: foo.slice is not a function

var foo = Object.values(foo); 

console.log(foo); // => [ 1, 2, 3, 4 ]

foo.push(5);

console.log(foo); // => [ 1, 2, 3, 4, 5 ]

回答by AmerllicA

There are some legacy methods but I prefer the ES6and newer versions, So if I wanna implement this, I wrote it like below:

有一些遗留方法,但我更喜欢ES6新版本,所以如果我想实现这个,我写如下:

const func = ...arg => console.log(arg);

Simple and cutting edge of tech.

简单而前沿的技术。

回答by Dary

Javascript ES5

Javascript ES5

function testArray(){
    for(index = 0; index < arguments.length; i++) {
        alert(arguments[index])
    }
}

Javascript ES6

Javascript ES6

const testArray = (...arg) => console.log(arg)

回答by Zoidberg

function toArray() {
   return arguments;
}

var myargs = toArray(1, 2, 3, 4, 5, 6);

The argumentskeyword is available in every js function

arguments关键字在每个 js 函数中都可用

回答by clyfish

var arr = [];
function testArray() {
    Array.prototype.push.apply(arr, arguments);
}