Currying: The technique of transforming a function that accepts multiple parameters into a function that accepts a single parameter (the first parameter of the original function), and returns a new function that accepts the remaining parameters and returns the result.
It's a Wikipedia explanation. Look a little confused, or use code to test it.
function currying(fn) {
if (arguments <= 0) {
return;
}
var args = Array.prototype.slice.call(arguments, 1);
return function () {
var innerArgs = Array.prototype.slice.call(arguments);
var totalArgs = args.concat(innerArgs);
return fn.apply(null, totalArgs);
}
}
function totalFee(price, number) {
return price * number;
}
var appleTotalFee = currying(totalFee, 8388);
console.log(appleTotalFee(2)); //16776
var huaWeiTotalFee = currying(totalFee, 3288);
console.log(huaWeiTotalFee(5)); //16440
A curring function is defined. The first parameter is the function that needs to be curried, and then other parameters are acquired. At the same time, the returned function combines the outer parameters with the inner parameters and transfers them through the application method. This paper defines a function to calculate the total price, which needs to pass a price and a number. An apple Total Fee and a huaWeiTotal Fee functions are generated by curring function, which can calculate the total price of iphone x and huawei p10 respectively. The results show that two iPhones can buy five huawei p10....
An application of function coritization (custom bind function)
The bind function appears in es5 and is not supported in ie6~ie8. So you need to customize a bind function for browser compatibility. Upper code
if(!Function.prototype.bind){
Function.prototype.bind = function (context) {
var that = this;
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return that.apply(context, args);
}
}
}
var name = 'limei';
var object = {
name: 'hanleilei'
}
function getName(sex) {
console.log(this.name + ' is a ' + sex);
}
getName('girl'); //limei is a girl
var bindFun = getName.bind(object,'boy');
bindFun(); //hanleilei is a boy
Another version
function curryedBind(fn, context) {
var args = Array.prototype.slice.call(arguments, 2);
return function () {
var innerArgs = Array.prototype.slice.call(arguments);
var totalArgs = args.concat(innerArgs);
return fn.apply(context, totalArgs);
}
}
var name = 'limei';
var object = {
name: 'hanleilei'
}
function getName(sex) {
console.log(this.name + ' is a ' + sex);
}
getName('girl');
var bindFun = curryedBind(getName, object, 'boy');
bindFun();
var bindFun2 = curryedBind(getName, object);
bindFun2('boy');
The two implementations are similar, but the first version binds the bind method to the prototype of Function without the bind method, and the second version defines a bind function that can be called directly.
I haven't used the function coritization in actual combat yet. It is estimated that there is no such application concept at the time of development, and there will be opportunities to use it in future projects.
over...