You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var lo = _([11,12,23,41,5,6,17,8]);
lo = lo.filter(item=>item>12);
lo = lo.filter(item=>item>20)
lo = lo.map(item=>30)
var values = lo.value();
console.log(values);
lodash惰性求值
什么叫惰性求值?
这个理论存编程语言层面, 如上所述, 有最小化求值, 和延迟求值的意思, 顾名思义, 非常容易理解.
关于惰性求值这里有一篇科普文
lodash 的惰性求值执行的流程
一个很平常的例子:
惰性求值是lodash的特性之一, 也是lodash一个比较喜人的特性, 可以有效地提高性能, 同时带来不一样的编程思考. 而它的实现也非常的简单, 思路是每一次调用lo.filter, lo.map等,都返回一个全新的lodash对象, 里面记录了所有的能够延迟求值的函数, 比如'filter', 'map', 'takeWhile'等等函数的调用.
lodash的lazyValue
调用value(); 即是调用lazyValue
那lazyValue 干了什么?
参数, 变量说明:
lazyValue函数就是开始执行了, 这里有几个主要的参数说明, 弄明白参数的作用, 那么这个函数的调用过程也就差不多
__wrapped__
, 这个是_([1,2,3]), 或_.chain([1,2,3]), 传进来的数组, 也是我们需要操作的值__dir__
, 这个是方向, 可以从右开始遍历, 默认是从左到右遍历, 从索引0开始__iteratees__
, 这个是.filter(item=>item>1), fillter, map等等的函数回调函数, 是个数组, 储存lazyValue之前的所有的链式调用方法的回调.getView()
, 这个是计算调用了take, drop等之后受印象的start, end, 是为了判断有没有调用take, drop等方法, 而进入①/②等start
, 遍历的起始end
, 遍历的结束takeCount
, 需要返回或者遍历的长度小结主要流程
常规流程①的调用过程
常规的调用的, 就是一个个串行的filter,map,find,等等的遍历的数组.上一个的输入是当前的输入.当时这里有一点点绕, 因为filter, map,take, 在lodash上也是一个个有实际意义的方法, 所以要做一些判断,
baseWrapperValue(array, this.__actions__);
arrayReduce(actions, prediction, result)
arrayReduce 迭代所有记录(filter,map,find等的调用, 类似reduce,上一个的输出是当前的输入. 完成后返回最后的结果
The text was updated successfully, but these errors were encountered: