1

R stands for the Ramda.js library, similar to the Underscore.js

var test = [
  {p1: 1}
];

var get = R.curry(function(prop, obj) { return obj[prop]; });
console.log(R.map(get('p1'), test));

what i know about the R.map callback that it takes only one argument but here we passed two arguments prop, obj and it's working ?

we didn't use obj so it should be undefined --> return undefined[prop] right ?

this code prove that map callback takes only one argument

var test = [
  {p1: 1}
];

function fn(arg1, arg2) {
  return arg1 + ' ' + arg2 + '\n';
}

console.log(R.map(fn, test));

the result is

["[object Object] undefined"]
4

1 回答 1

3

这是有效的,因为get它是一个咖喱函数。这意味着get('p1')返回一个接受一个参数的函数。在这种情况下,它返回如下函数:

function(obj) { return obj['p1']; });

这允许它被传递给R.map函数。

为了理解柯里化,维基百科的文章相当出色:https ://en.wikipedia.org/wiki/Currying

于 2015-12-21T02:03:58.857 回答