myUnderscore
  • myUnderscore
  • 基础篇-将函数挂载到'_'上
    • 函数式风格调用
    • 实现面向对象风格调用。
    • 链式调用
  • 类数组与最大安全数
  • 函数判断与Js中的’&& ||‘+假值
  • 字符串反转
  • 高阶函数篇
    • every和some
    • sortBy和闭包
    • 用于调试的tap()
    • unary和parseInt的经典大坑
    • once 和 它牵扯出的this
    • this
      • 调用位置和绑定规则
      • 隐式绑定与隐式丢失
      • 显式绑定与硬绑定
      • new绑定和优先级
      • 被忽略的this和更安全的this用法
      • 软绑定和箭头函数的this
      • 遗留问题:硬绑定无法被改绑的原理是什么?
  • memoized 函数
  • forEach,map 和 reduce
  • filter 过滤器 和 zip 合并数组
  • flatten数组扁平化
  • 避免XXS攻击的字符转义(escape)和反转义(unescape)
Powered by GitBook
On this page

Was this helpful?

  1. 高阶函数篇

用于调试的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
    })
)
PrevioussortBy和闭包Nextunary和parseInt的经典大坑

Last updated 5 years ago

Was this helpful?