[英]Arguments Array in JavaScript (ES5)
Maybe that is not my evening :/ Very simple thing, I want to give an array as parameter to a function as arguments array: 也许那不是我晚上:/非常简单的事情,我想将一个数组作为参数提供给作为参数数组的函数:
function add() {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
The following works: 以下作品:
console.log(add(1,2,3,4,5,6));
But if I fill an array and give it as parameter, like: 但是,如果我填充一个数组并将其作为参数,例如:
var myNumbers = [];
for (var i=0; i<100; i++){
myNumbers.push(i);
}
console.log(add(myNumbers));
I get trouble. 麻烦了 I think, I miss something important about the
arguments
array. 我想,我错过了有关
arguments
数组的重要信息。
How should I change the add function, so that both possibilities can work with it? 我应该如何更改添加功能,以便这两种可能性都可以使用?
The arguments
object is an array-like object, but it is not an array. arguments
对象是类似数组的对象,但不是数组。 It is used to represent all arguments passed into the function. 它用于表示传递给函数的所有参数。 You have only passed in one value into the function, so your array is actually at index 0 of the
arguments
object. 您仅将一个值传递给该函数,因此您的数组实际上位于
arguments
对象的索引0处。
However, there really isn't much point using arguments
here unless you need to dynamically handle things without defining an explicit API. 但是,在这里使用
arguments
确实没有多大意义,除非您需要在不定义显式API的情况下动态处理事物。 Just declare your parameter in the add
function. 只需在
add
函数中声明您的参数即可。
function add(arr) {
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
If you want to support both cases, as per your comment, you can do something like: 如果您想同时支持这两种情况,请按照您的评论进行操作:
function add() {
var arr = [].concat.apply([], arguments);
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
Explanation of [].concat.apply([], arguments)
的说明
[].concat.apply([], arguments)
[].
is shorthand for Array.prototype
because it's an empty array. Array.prototype
简写,因为它是一个空数组。 concat
merges two or more arrays together, or an array and values to go into the array. concat
将两个或多个数组合并在一起,或者将一个数组和值合并到该数组中。 arguments
is not an array, but many of the prototype functions will work on it, due to some array-like characteristics of the object - indexed items and the length
property. arguments
不是数组,但是由于对象的某些类似于数组的特性-索引项和length
属性,许多原型函数都可以在其上使用。 apply
calls a function with a given context (the this
binding) and any number of arguments. apply
调用具有给定上下文( this
绑定)和任意数量参数的函数。 In this case we still want to use the array as this
value to be able to call concat
, followed by all the arguments we passed into add
. this
值,以便能够调用concat
,然后再调用传递给add
所有参数。 The result is simply all arguments as a proper array. This solution works for both situation : 此解决方案适用于两种情况:
function add() {
var arr= Array.prototype.slice.call(arguments);
arr = [].concat.apply([], arr);
var sum = 0;
for (var i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
Also simple solution for sum : sum的简单解决方案:
function add() {
var arr= Array.prototype.slice.call(arguments);
arr = [].concat.apply([], arr);
return arr.reduce(function(f, s){return f + s;}, 0);
}
Here: 这里:
add(1,2,3,4,5,6);
...you're calling add
with a series of discrete (separate) arguments, and each of them shows up in the arguments
pseudo-array, 1 at arguments[0]
, 2 at arguments[1]
, etc. ......你调用
add
与一系列的离散 (分离)的参数,并且在它们中的每显示出来arguments
伪阵列,在1 arguments[0]
在2 arguments[1]
等等
But here: 但在这儿:
add(myNumbers);
...you're calling add
with one argument, which is an array. ...您正在用一个参数(数组)调用
add
。 That's at arguments[0]
. 那是在
arguments[0]
。
You'll want to write add
to fit how you want to call it. 您将要编写
add
以适合您的调用方式。 If you want to call it with discrete arguments, write it the way you have. 如果要使用离散参数调用它,请按照自己的方式编写。 If you want to write it to accept an array, have it take a single argument and loop through that (which will be the array):
如果要编写它来接受数组,请让它接受一个参数并遍历该参数(将是数组):
function add(args) {
// ^------------- the one argument
var sum = 0;
for (var i = 0; i < args.length; i++) {
sum += args[i];
}
return sum;
}
If you want to handle both , you could use Array.isArray
(newish, but shimmable) to check the first argument to see if it's an array and either use it if it is or use arguments
if it isn't: 如果要同时处理两者 ,则可以使用
Array.isArray
( Array.isArray
但可调整)来检查第一个参数,以查看它是否为数组,如果是,则使用它;如果不是,则使用arguments
:
function add(args) {
if (!Array.isArray(args)) {
args = arguments;
}
var sum = 0;
for (var i = 0; i < args.length; i++) {
sum += args[i];
}
return sum;
}
Side note: arguments
isn't an array, it's just array-like . 旁注:
arguments
不是数组,而是类似数组的 。
您可以使用申请
add.apply(null, [1,2,3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.