对象在node.js中使用参数时没有方法“减少”错误?

时间:2022-08-19 18:29:06

Why do I get an error when using arguments like this?

为什么在使用这样的参数时会出现错误?

function sum(){
    return arguments.reduce(function(a,b){
        console.log(a+b)
        return a+b;
    },0);
}

sum(1,2,3,4);

error:

错误:

/Users/bob/Documents/Code/Node/hello.js:2
return arguments.reduce(function(a,b){
                 ^
TypeError: Object #<Object> has no method 'reduce'
    at sum (/Users/bob/Documents/Code/Node/hello.js:2:19)
    at Object.<anonymous> (/Users/bob/Documents/Code/Node/hello.js:8:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:903:3

This is from Mr. Crockford's JS lectures.

这是Crockford先生的JS讲座。

3 个解决方案

#1


27  

arguments is not a real array, it's an "array-like" object and reduce is not a method of array-like objects. You can use reduce by passing arguments as context, like this:

参数不是一个真正的数组,它是一个“类数组”对象,reduce不是类数组对象的方法。您可以使用reduce作为上下文传递参数,比如:

[].reduce.call(arguments, function(a, b) {

});

Edit: more info on array-like objects here at the MDN.

编辑:更多关于类数组对象的信息请访问MDN。

#2


1  

Crockford explicitly states that using Array methods such as reduce() on arguments was introduced in ECMAscript 5. Before ECMAscript5, not even Array had reduce() in all Javascript implementations. For things like map() and reduce(), I recommend using a library like Underscore which hides the implementation differences.

Crockford明确指出,在ECMAscript 5中引入了对参数使用reduce()等数组方法。在ECMAscript5之前,甚至在所有Javascript实现中都没有reduce()。对于map()和reduce()之类的东西,我建议使用下划线这样的库来隐藏实现差异。

#3


0  

You get an error because arguments is an object not a list. Consider the following:

你会得到一个错误,因为参数是一个对象而不是一个列表。考虑以下:

> function a(){ return arguments; }
> b = a(1, 2, 3);
> b
{ '0': 1,
  '1': 2,
  '2': 3 }

The MDN JavaScript documentation for arguments has more information, including:

用于参数的MDN JavaScript文档有更多信息,包括:

An Array-like object corresponding to the arguments passed to a function.

与传递给函数的参数相对应的数组类对象。

#1


27  

arguments is not a real array, it's an "array-like" object and reduce is not a method of array-like objects. You can use reduce by passing arguments as context, like this:

参数不是一个真正的数组,它是一个“类数组”对象,reduce不是类数组对象的方法。您可以使用reduce作为上下文传递参数,比如:

[].reduce.call(arguments, function(a, b) {

});

Edit: more info on array-like objects here at the MDN.

编辑:更多关于类数组对象的信息请访问MDN。

#2


1  

Crockford explicitly states that using Array methods such as reduce() on arguments was introduced in ECMAscript 5. Before ECMAscript5, not even Array had reduce() in all Javascript implementations. For things like map() and reduce(), I recommend using a library like Underscore which hides the implementation differences.

Crockford明确指出,在ECMAscript 5中引入了对参数使用reduce()等数组方法。在ECMAscript5之前,甚至在所有Javascript实现中都没有reduce()。对于map()和reduce()之类的东西,我建议使用下划线这样的库来隐藏实现差异。

#3


0  

You get an error because arguments is an object not a list. Consider the following:

你会得到一个错误,因为参数是一个对象而不是一个列表。考虑以下:

> function a(){ return arguments; }
> b = a(1, 2, 3);
> b
{ '0': 1,
  '1': 2,
  '2': 3 }

The MDN JavaScript documentation for arguments has more information, including:

用于参数的MDN JavaScript文档有更多信息,包括:

An Array-like object corresponding to the arguments passed to a function.

与传递给函数的参数相对应的数组类对象。