How to understand the currying
function?
How the newSum
and newFind
works?
var currying = function(fn) { var args = []; return function() { if (!!arguments.length) { [].push.apply(args, arguments); // What's the meaning of this writing? return arguments.callee; } else { return fn.apply(this, args); } } } // accumulation currying var sum = (function(num){ var ret = 0; return function(){ for(var i = 0, len = arguments.length; i < len; i++) { ret += arguments[i]; } return ret; } })(); var newSum = currying(sum); newSum(1)(2)(3)(4)() // 10 // element find currying var find = function(arr, el){ return arr.indexOf(el) !== -1; } var newFind = currying(find)([1,2,3]); newFind(1); newFind(2);
The currying
function, and gets a function as an argument, and returns a new function, that when invoked:
args
array So, if we look at this call for example: newSum(1)(2)(3)(4)()
- there are 5 function invocations:
newSum
with 1
. Getting the curried function with 1 accumulated. sum
function - and getting the correct value, 10. About [].push.apply(args, arguments);
and fn.apply(this, args);
: apply
is a method on Function.prototype
that basically lets you call a function, supplying a context object and an array of arguments. So basically [].push.apply(...)
is a trick of concatenating an array into another array, in our case, concat arguments
(which is the list of arguments provided to a function upon invocation) into args
(the accumulating list of arguments). fn.apply(this, args);
is simply calling the original function with all the accumulated arguments. You can read more about it in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
By the way, newFind(1);
and newFind(2);
both return a function, that will look for the index of the element only when invoked, meaning newFind(1)() === true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.