<分区>
我正在尝试找到一种方法来使用 JS 的 Array.prototype.map()
功能,该函数具有一个附加参数 more(如果可能的话,我想避免必须重写内置的 Array.prototype.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
到目前为止,还不错。 但是,如果所讨论的函数有两个 参数怎么办,比如 e。 G。您可能想要对其进行或操作的标志(想想位掩码)
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!
当然,任何解决方案都应该没有 for
循环,因为这就是为什么我们有 map()
,以使我们的代码更清晰 w/摆脱这些!