用于调试的tap()

假设你在遍历一个来自服务器的数组,并发现数据错了.因此你想调试一下,看看你数组里究竟包含了什么. 不要使用命令式的方法,要用函数式的方法.我们需要一种调试方式.

_.tap = (value)=>
        (fn)=>{
                typeof(fn) === 'function' && fn(value);
                console.log(value);
        }

我们可以这样用:

_.forEach([1,2,3],(a)=>
    _.tap(a)(()=>{
        console.log(a);//1 1 2 2 3 3
    })
)

Last updated