Javascript 使用带有一个附加参数的函数的 map() 的 JS 回调

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

JS callback using map() with a function that has one additional parameter

javascriptarraysmapparameterscallback

提问by syntaxerror

I'm trying to find a way to use JS's Array.prototype.map()functionality with a function that has one additional parameter more (if possible at all, and I'd like to avoidhaving to rewrite built-in Array.prototype.map()). This documentation is very good, but does not cover the "one-or-more-additional-parameter" case:

我试图找到一种方法来使用 JS 的Array.prototype.map()功能,该函数还有一个额外的参数(如果可能的话,我想避免重写 built-in Array.prototype.map())。这个文档非常好,但不包括“一个或多个附加参数”的情况:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map

function doOpSingle(elem)
{
 // do something with one array element
}

var A = ["one", "two", "three", "four"];
var x = A.map(doOpSingle); // this will execute doOpSingle() on each array element

So far, so good. But what if the function in question has twoparameters, like e. g. a flag you might want to OR to it (think of bit masks)

到现在为止还挺好。但是如果有问题的函数有两个参数,比如一个你可能想要对其进行 OR 的标志(想想位掩码)怎么办

function doOpSingle2(arrelem,flag)
{
 // do something with one array element
}

var A = ["one", "two", "three", "four"];
var theFlag = util.getMask(); // call external function
var y = A.map(doOpSingle2(theFlag)); // this does not work!

Any solutions should do withoutforloops, of course, because that's why we have map(), to make our code cleaner w/getting rid of these!

当然,任何解决方案都应该没有for循环,因为这就是为什么我们有map(), 使我们的代码更干净,并且摆脱这些!

回答by Niet the Dark Absol

Use an anonymous function:

使用匿名函数:

A.map(function(a) {return doOpSingle2(a,theFlag);});

回答by Esailija

A.map(doOpSingle2.bind(null, theFlag))

A.map(doOpSingle2.bind(null, theFlag))

theFlagwill be first argument though:

theFlag不过将是第一个参数:

function doOpSingle2( flag, elem ) { ... }

function doOpSingle2( flag, elem ) { ... }

回答by MatrixFrog

Edit: Nevermind, you don't really need Closure for this. See Esailija's answer.

编辑:没关系,你真的不需要闭包。请参阅 Esailja 的回答。

There is another option, if you happen to be using the Closure library. First you have to rewrite doOpSingle2 so that the flag is first:

如果您碰巧使用Closure 库,还有另一种选择。首先你必须重写 doOpSingle2 以便标志是第一个:

function doOpSingle2(flag, arrelem)
{
 // do something with one array element
}

then you can do

那么你可以做

var y = A.map(goog.partial(doOpSingle2, theFlag));

goog.partial partially appliesthe function, so goog.partial(doOpSingle2, theFlag)is equivalent to this function:

goog.partial部分应用了这个函数,所以goog.partial(doOpSingle2, theFlag)等价于这个函数:

function(arrElem) {
  return doOpSingle2(theFlag, arrElem);
}

In this particular case, I probably wouldn't recommend this approach, but there might be similar situations where it works well.

在这种特殊情况下,我可能不会推荐这种方法,但可能有类似的情况下它运行良好。

回答by UltraInstinct

Use an anonymous function.

使用匿名函数。

var y = A.map(function(x){doOpSingle2(x,theFlag);});