baseSum
"Let go of the thoughts that don't make you strong." —Unknown
本文为 《lodash 源码阅读》 系列文章,后续内容会在 github 中发布,欢迎 star,gitbook 同步更新。
源码
/**
* sum 跟 sumBy 的基础实现
*
* @private
* @param {Array} array 要迭代的数组。
* @param {Function} iteratee 调用每个元素的迭代函数。
* @returns {number} 返回总和。
*/
function baseSum(array, iteratee) {
let result;
for (const value of array) {
const current = iteratee(value);
if (current !== undefined) {
result = result === undefined ? current : result + current;
}
}
return result;
}
原理
baseSum
使用 for...of
遍历 array
,这样做的好处是可以兼容自定义 Symbol.iterator
的 array
参数以及更多的类型(例如: String/TypeArray/Map/Set/arguments对象/DOM集合
等);再通过传入的 iteratee
方法取得当前值 current
,将合法的 current
计入 result
中,最终计算返回总值结果。
相关链接
Last updated
Was this helpful?