最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

function - Why does Javascript's array map callback require extra arguments - Stack Overflow

programmeradmin3浏览0评论

Say I have:

myArray = [1,2,3];

And say I have a util function somewhere else:

add3 = function(val) {
   return val+3;
}

Now if I want to use that as the call back for a map function, I have to do something like this:

add3Callback = function(currentValue, index, array) {
    return add3(currentValue);
}

myArray.map(add3Callback);

It would be better that the map callback doesn't require the extra two arguments, because now I have to add wrappers to all my util functions in order to use them with map, which goes against some of the reasons for using map in the first place.

What am I missing here? Why is having those arguments useful, other languages don't seem to do it.

Say I have:

myArray = [1,2,3];

And say I have a util function somewhere else:

add3 = function(val) {
   return val+3;
}

Now if I want to use that as the call back for a map function, I have to do something like this:

add3Callback = function(currentValue, index, array) {
    return add3(currentValue);
}

myArray.map(add3Callback);

It would be better that the map callback doesn't require the extra two arguments, because now I have to add wrappers to all my util functions in order to use them with map, which goes against some of the reasons for using map in the first place.

What am I missing here? Why is having those arguments useful, other languages don't seem to do it.

Share Improve this question edited May 1, 2015 at 6:29 Cameron Ball asked May 1, 2015 at 6:28 Cameron BallCameron Ball 4,1286 gold badges26 silver badges36 bronze badges 1
  • 1 It doesn't require any extra arguments, you can do myArray.map(add3), same result. – elclanrs Commented May 1, 2015 at 6:28
Add a ment  | 

3 Answers 3

Reset to default 6

map provides these arguments for convenience, but they are not required, you'd get the same result with:

myArray.map(add3)

In case you have a function that can take more than one argument you can restrict it to a single argument:

myArray.map(unary(console.log))

where:

function unary(f) {
  return function(x) {
    return f(x)
  }
}

You can do this with binary, ternary, nary...

Note that no function in JS requires any arguments to be passed by name, doing:

function f(a, b, c) {
  return [a, b, c]
}

is the same as:

function f() {
  return [arguments[0], arguments[1], arguments[2]]
}

Your callback function is not required to accept extra arguments. map will pass three arguments, but your function is entirely free to ignore them and not even list them in the parameter list.

This is perfectly valid:

myArray.map(function (currentValue) {
    return add3(currentValue);
});

And therefore so is this:

myArray.map(add3);

You do not have to provide the extra parameters. In fact you do not have to provide any paramters at all. There is the arguments object in JS, which has information of provided arguments for the function.

slice=Function.prototype.call.bind([].slice);
function fun(){
    slice(arguments).forEach(function(x){ console.log(x);})
}

fun(1,2,3,4);

Btw. arguments is one of those strange objects in JS: it behaves arraylike though not beeing an array. You could use [] to access elements, but e.g. no forEach.

So

function add3(){
    return arguments[0]+3;
}

console.log([1,2,3].map(add3))

is valid.

function add3(a,b,c,d){
    return a+3;
}

is valid too, though b,c,d are not needed. If you put this into the map function

function add3(a,b,c,d){
    console.log(d)
    return a+3;
}

d is not used/called by map() and therefore undefined You could say, that the parameters are optional or an "offer" to use, but there is no obligation to do so.

JS behaves in this regard a bit like Perl.

发布评论

评论列表(0)

  1. 暂无评论