R代表Ramda.js库,类似于Underscore.js
var test = [
{p1: 1}
];
var get = R.curry(function(prop, obj) { return obj[prop]; });
console.log(R.map(get('p1'), test));
我对 R.map 回调的了解是它只接受一个参数,但在这里我们传递了两个参数 prop, obj 并且它起作用了?
we didn't use obj so it should be undefined --> return undefined[prop] right ?
这段代码证明 map 回调只接受一个参数
var test = [
{p1: 1}
];
function fn(arg1, arg2) {
return arg1 + ' ' + arg2 + '\n';
}
console.log(R.map(fn, test));
结果是
["[object Object] undefined"]
最佳答案
这是可行的,因为 get
是一个柯里化(Currying)函数。这意味着 get('p1')
返回一个接受一个参数的函数。在这种情况下,它会返回如下函数:
function(obj) { return obj['p1']; });
这允许将其传递给 R.map
函数。
要了解柯里化(Currying),维基百科文章相当出色:https://en.wikipedia.org/wiki/Currying
关于javascript - 将两个参数传递给 .map 回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34387799/