lodash流函数有什么作用? [英] What does the lodash flow function do?

查看:80
本文介绍了lodash流函数有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从lodash中读取一些使用_.flow()的代码,而文档中的解释对我来说却没有任何意义.

I am reading some code that uses _.flow() from lodash and the explanation in the docs just isn't making sense to me.

医生说

创建一个函数,该函数返回调用给定结果的结果 函数与已创建函数的this绑定,其中每个 连续的调用将提供前一个的返回值.

Creates a function that returns the result of invoking the given functions with the this binding of the created function, where each successive invocation is supplied the return value of the previous.

以示例为例:

function square(n) {
  return n * n;
}

var addSquare = _.flow([_.add, square]);
addSquare(1, 2);
// => 9

我已经读过几次了,我无法弄清楚它的含义或该函数如何返回9.我能想到的最接近的想法是在函数式编程中折叠起来,但这看起来并不是那样.有什么其他方法可以解释流量的变化吗?

I have read this a few times and I can not work out what it means or how that function returns 9. The closest think I can think of is folding in functional programming but this doesn't look like that. Is there any alternative way of explaining what flow does?

推荐答案

用更简单的术语重新定义文档中的定义:依次调用数组中的方法.它使用每个函数的结果作为下一个函数的参数.在给出示例的情况下,步骤如下:

Rewording the definition from the docs in simpler terms: It calls, in order, the methods in the array. It uses the result from each function as the parameters for the next function. In the case of the example given, the steps are as follows:

  1. 调用_.add(1, 2),它返回3.
  2. 将该返回值作为参数传递给数组中的下一个函数,该函数成为square(3).这将返回9.
  1. Invokes _.add(1, 2), which returns 3.
  2. Passes that return value as the parameter to the next function in the array, which becomes square(3). This returns 9.

这篇关于lodash流函数有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆