简体   繁体   English

js currying函数示例

[英]js currying function example

How to understand the currying function? 如何理解currying函数?

How the newSum and newFind works? newSumnewFind如何工作?

 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: currying函数,并获取一个函数作为参数,并返回一个新函数,该函数在被调用时:

  • If arguments are provided, they are accumulated in the args array 如果提供了参数,则它们会累积在args数组中
  • If arguments are not provided, the original function is called with all accumulated arguments. 如果未提供参数,则使用所有累积的参数调用原始函数。

So, if we look at this call for example: newSum(1)(2)(3)(4)() - there are 5 function invocations: 因此,如果我们看一下此调用,例如: newSum(1)(2)(3)(4)() -有5个函数调用:

  • Calling newSum with 1 . 1调用newSum Getting the curried function with 1 accumulated. 累计获得1的咖喱函数。
  • Calling the curried function with 2 - getting the same function with 1 and 2 accumulated, and so on for 3 and 4. 用2调用curried函数-用1和2累加得到相同的函数,以此类推3和4。
  • Calling the curried function without arguments - applying all the accumulated arguments (1, 2, 3, 4) to the original sum function - and getting the correct value, 10. 调用不带参数的curried函数-将所有累积的参数(1、2、3、4)应用于原始sum函数-并获取正确的值10。

About [].push.apply(args, arguments); 关于[].push.apply(args, arguments); and fn.apply(this, args); 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. applyFunction.prototype上的一种方法,基本上可以让您调用一个函数,提供一个上下文对象和一个参数数组。 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). 因此,基本上[].push.apply(...)是将一个数组连接到另一个数组的技巧,在我们的例子中,将concat arguments (这是调用时提供给函数的参数列表)到args (累积列表)中的论点)。 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 您可以在https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply中了解有关此内容的更多信息

By the way, newFind(1); 顺便说一下, newFind(1); and newFind(2); newFind(2); both return a function, that will look for the index of the element only when invoked, meaning newFind(1)() === true 都返回一个函数,该函数仅在被调用时才查找元素的索引,这意味着newFind(1)() === true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM