Underscore.js _.rcurry2() 方法
Underscore.js _.rcurry2() 方法返回给定函数的柯里化版本,其中从右到左最多处理两个参数。
句法:
_.rcurry2( fun )
参数:此方法采用上面列出并在下面讨论的单个参数:
- fun:这是作为参数传递的给定函数。
返回值:此方法返回一个柯里函数。
注意:要执行以下示例,您必须使用此命令提示符安装underscore-contrib库并执行以下命令。
npm install underscore-contrib
示例 1:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function div(a, b) {
return a/b;
}
var curried = _.rcurry2(div);
console.log("Curried division is :",
curried(4)(16));
输出:
Curried division is : 4
示例 2:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function sub(a, b) {
return a-b;
}
var curried = _.rcurry2(sub);
console.log("Curried Subtraction is :",
curried(2)(4));
输出:
Curried Subtraction is : 2
示例 3:反转参数如下所示:
// Defining underscore contrib variable
var _ = require('underscore-contrib');
// Function
function div(a, b) {
return ("a="+a+" and b="+b);
}
var curried = _.rcurry2(div);
console.log(curried("a")("b"));
输出:
a=b and b=a