Redux源码分析之compose
解读之前先了准备一下基本知识
形式为...变量名,
用于获取函数的多余参数 ,该变量将多余的参数放入数组中, 只能是参数的最后一个。
扩展运算符(spread)是三个点(...
)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。
var fun1 = (...args) => console.log(...args)
fun1(1,'Good')
// 结果输出 1, Good
这是一个很重要的函数 compose, 其作用红色部分表达的很清楚。
其作用是把一系列的函数,组装生成一个新的函数,并且从后到前,后面参数的执行结果作为其前一个的参数。
/**
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/ export default function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
} if (funcs.length === 1) {
return funcs[0]
} return funcs.reduce((a, b) => (...args) => a(b(...args)))
}
还是先来看一个简单的例子,结果上确实是如我们期望的。
var fn1 = val => 'fn1-' + val
var fn2 = val => 'fn2-' + val
var fn3 = val => 'fn3-' + val compose(fn1,fn2,fn3)('测试') 输出结果: fn1-fn2-fn3-测试
我们回头看看最核心的一行代码。
return funcs.reduce((a, b) => (...args) => a(b(...args)))
我们一步一步分解一下, func3 初始化的值为 funcs = [fn1,fn2,fn3], reduce执行
第一步时
a = fn1
b = fn2
a(b(...args)) = fn1(fn2(...args))
(...args) => a(b(...args)) = (...args) = > fn1(fn2(...args))
第二步时
a = (...ag) = > fn1(fn2(...ag)) // 避免和后面混淆,rest参数名修改为 ag
b = fn3
a(b(...args)) = a( fn3(...args) ) = fn1(fn2( fn3(...args) )) // 这一步是关键,b(...args)执行后作为a函数的入参,也就是 ...ag = fn3(...args)
(...args) => a(b(...args)) = (...args) = > fn1(fn2(fn3(...args)))
所以最后返回的就是这样的一个函数 (...args) = > fn1(fn2(fn3(...args)))
再多函数,也是以此类推。
引用:
Redux源码分析之compose的更多相关文章
- Redux源码分析之createStore
接着前面的,我们继续,打开createStore.js, 直接看最后, createStore返回的就是一个带着5个方法的对象. return { dispatch, subscribe, getSt ...
- Redux源码分析之applyMiddleware
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- Redux源码分析之基本概念
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- Redux源码分析之bindActionCreators
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- Redux源码分析之combineReducers
Redux源码分析之基本概念 Redux源码分析之createStore Redux源码分析之bindActionCreators Redux源码分析之combineReducers Redux源码分 ...
- redux源码阅读之compose,applyMiddleware
我的观点是,看别人的源码,不追求一定要能原样造轮子,单纯就是学习知识,对于程序员的提高就足够了.在阅读redux的compose源码之前,我们先学一些前置的知识. redux源码阅读之compose, ...
- 史上最全的 Redux 源码分析
前言 用 React + Redux 已经一段时间了,记得刚开始用Redux 的时候感觉非常绕,总搞不起里面的关系,如果大家用一段时间Redux又看了它的源码话,对你的理解会有很大的帮助.看完后,在回 ...
- 正式学习React(四) ----Redux源码分析
今天看了下Redux的源码,竟然出奇的简单,好吧.简单翻译做下笔记: 喜欢的同学自己可以去github上看:点这里 createStore.js import isPlainObject from ' ...
- Redux源码学习笔记
https://github.com/reduxjs/redux 版本 4.0.0 先了解一下redux是怎么用的,此处摘抄自阮一峰老师的<Redux 入门教程> // Web 应用是一个 ...
随机推荐
- Array.apply(null,{length:20})与new Array(20)的区别
Array.apply(null,{length:20}) 这句代码的实际意义:创建长度为20的一个数组,但并非空数组. 跟new Array(20)的区别在于,前一种创建方式,得到的数组中的每一个元 ...
- jQuery星级评分插件
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta http-equiv="Con ...
- Chapter 1. Introduce
前言 本书全名是<H.264 and MPEG-4 Video Compression, Video Coding For Next-generation Multimedia>,作者为 ...
- 如何在自己的网页上插入一个超链接,发起临时qq会话
1.先开通临时会话功能 打开网页http://shang.qq.com/v3/index.html
- Spring MVC 项目搭建 -6- spring security 使用自定义Filter实现验证扩展资源验证,使用数据库进行配置
Spring MVC 项目搭建 -6- spring security使用自定义Filter实现验证扩展url验证,使用数据库进行配置 实现的主要流程 1.创建一个Filter 继承 Abstract ...
- Windows PowerShell 默认颜色
屏幕背景:1,36,86 屏幕文字:238,237,240 弹出文字:0,128,128 弹出窗口背景:255,255,255
- VB6之SendMessage模拟拖放事件
原文链接:http://hi.baidu.com/coo_boi/item/e1e0f5ab45bddbdd5af191df 网上找了个C++的翻一下,原文链接:http://www.cnblogs. ...
- 使用Webpack加速Vue.js应用的4种方式
Webpack是开发Vue.js单页应用程序的重要工具. 通过管理复杂的构建步骤,你可以更轻松地开发工作流程,并优化应用程序的大小和性能. 其中介绍下面四种方式: 单个文件组件 优化Vue构建 浏览器 ...
- HDU 2255 奔小康赚大钱(带权二分图最大匹配)
HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...
- Gym 100952 D. Time to go back(杨辉三角形)
D - Time to go back Gym - 100952D http://codeforces.com/gym/100952/problem/D D. Time to go back time ...