我有这段代码:
var fn = function(fn, name) {
this.commands.add(name, fn);
}.bind(this);
_.mapObjIndexed(fn, this.commandList);
我觉得有一种方法可以改进这段代码并将其简化为一行。我尝试了很多不同的方法,但我是 ramda.js
的新手,我可能会遗漏一些功能,这将使这变得更容易和更简单。
最佳答案
您似乎在寻找 flip
function/ flip
function :
_.mapObjIndexed(_.flip(this.commands.add), this.commandList);
如果 .bind(this.commands)
对于 add
功能是必需的,您可以使用 <a href="https://lodash.com/docs#bindKey" rel="noreferrer noopener nofollow">_.bindKey</a>(this.commands, "add")
缩短它.
如果由于参数数量的原因翻转无法正常工作,您可能需要输入 _.ary(…, 2)
或 _.binary(…)
.
关于javascript - 我怎样才能使这段代码在 Ramda.js/lodash 中更像 'functional programming' 风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36432868/