Lodash - curry 方法

语法

_.curry(func, [arity=func.length])

创建一个接受 func 参数的函数,如果至少提供了 arity 个参数,则调用 func 返回其结果,或者返回接受其余 func 参数的函数,依此类推。如果 func.length 不足,可以指定 func 的 arity。

参数

  • func (函数) − 要 curry 的函数。

  • [arity=func.length] (number) − func 的参数数量。

输出

  • (Function) − 返回新的柯里化函数。

示例

var _ = require('lodash');
var getArray = function(a, b, c) {
    return [a, b, c];
};

var curried = _.curry(getArray);
console.log(curried(1)(2)(3));
console.log(curried(1, 2)(3));
console.log(curried(1, 2, 3));

将上述程序保存在 tester.js 中。运行以下命令执行此程序。

命令

\>node tester.js

输出

[ 1, 2, 3 ]
[ 1, 2, 3 ]
[ 1, 2, 3 ]

lodash_function.html