Skip to content

Latest commit

 

History

History
43 lines (28 loc) · 1.67 KB

reduceRight.md

File metadata and controls

43 lines (28 loc) · 1.67 KB

lodash源码分析之reduceRight

本文为读 lodash 源码的第一百五十八篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash

gitbook也会同步仓库的更新,gitbook地址:pocket-lodash

依赖

import arrayReduceRight from './.internal/arrayReduceRight.js'
import baseEachRight from './.internal/baseEachRight.js'
import baseReduce from './.internal/baseReduce.js'

《lodash源码分析之arrayReduceRight》 《lodash源码分析之baseEachRight》 《lodash源码分析之baseReduce》

源码分析

reduceRightreduce 作用类似,不过是从后向前遍历。

源码如下:

function reduceRight(collection, iteratee, accumulator) {
  const func = Array.isArray(collection) ? arrayReduceRight : baseReduce
  const initAccum = arguments.length < 3
  return func(collection, iteratee, accumulator, initAccum, baseEachRight)
}

可以看到,和 reduce 不一样的地方是,如果是 array,则 funcarrayReduceRightbaseEach 也用 baseEachRight 替代。

其实就是将对应的方法替换成 xxxRight 的方法。

License

署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)

最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:

作者:对角另一面