Lodash _.fix() 方法
Lodash _.fix()方法根据由值的存在和 _占位符定义的参数模板将参数固定到函数。
该函数采用在给定值中替换为 _ 的参数。
句法:
_.fix( fun, [values] )
参数:此方法采用上面列出并在下面讨论的单个参数。
- fun:此参数保存给定的函数。
- values:传递给 fun 的值。
返回值:它返回一个新函数。
注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。
npm install lodash-contrib
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function
function mul(a, b, c, d){
return a*b*c*d;
}
// Making curried function
var gfgFunc = _.fix(mul, 1, 2, 3, _);
// 8 is replaced by _ in given values
console.log("Multiplication is :", gfgFunc(8));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function
function add(a, b, c){
return a + b + c;
}
// Making fixed function
var gfgFunc = _.fix(add, 1, 2, _);
console.log("Addition is :", gfgFunc(10));
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function
function fun(str){
return str;
}
// Making fixed function
var gfgFunc = _.fix(fun, _);
console.log("Coding Platform :",
gfgFunc("GeeksforGeeks"));
输出:
Multiplication is : 48
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function
function add(a, b, c){
return a + b + c;
}
// Making fixed function
var gfgFunc = _.fix(add, 1, 2, _);
console.log("Addition is :", gfgFunc(10));
输出:
Addition is : 13
示例 3:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Function
function fun(str){
return str;
}
// Making fixed function
var gfgFunc = _.fix(fun, _);
console.log("Coding Platform :",
gfgFunc("GeeksforGeeks"));
输出:
Coding Platform : GeeksforGeeks