underscore-analysis
underscore-analysis copied to clipboard
underscore 源码分析
解读 cb 函数
继续阅读 underscore 源码: ``` var builtinIteratee; // An internal function to generate callbacks that can be applied to each // element in a collection, returning the desired result — either...
underscore 源码里面会常常需要用到对于 this 值的硬绑定, 其中让我们头疼的,其实是不定传参,虽然说我们可以通过对 argument 对象使用 slice 方法转为数组,但是在函数内部对 arguments 进行分割无疑代码耦合了, 因为 rest 数组的生成其实与具体函数的逻辑并无关系,所以 underscore 就采用一个通用的 restArgs 方法来进行对函数包装, 使之支持 rest 参数,个人认为这也是 underscore 函数式编程思想的体现之一。 源码中有下面这段代码: ``` // Similar to ES6's rest...
本系列通过阅读 underscore 源码与实战进而体验函数式编程的思想, 而非通过冗长的文字教程, 细读精度 约 1500 行的 underscore 有利于写出耦合度低, 符合函数式编程思想的代码, 并且可以学到 call 与 apply 执行效率的不同进而进行代码性能优化的技巧等. 欢迎大家 star 或者 watch 本系列, 您的关注是作者的最大动力, 让我们一起持续进步. 本系列仓库: https://github.com/zhangxiang958/underscore-analysis ## 兼容 for...in 循环 我们在遍历对象的时候往往会使用...
本系列通过阅读 underscore 源码与实战进而体验函数式编程的思想, 而非通过冗长的文字教程, 细读精度 约 1500 行的 underscore 有利于写出耦合度低, 符合函数式编程思想的代码, 并且可以学到 call 与 apply 执行效率的不同进而进行代码性能优化的技巧等. 欢迎大家 star 或者 watch 本系列, 您的关注是作者的最大动力, 让我们一起持续进步. 本系列仓库: https://github.com/zhangxiang958/underscore-analysis ## Typeof 我们都使用 typeof 是用来判断数据类型的命令,...
从头开始阅读源码: ## 使用 IIFE underscore 将所有函数封装在一个 IIFE 里面, 避免污染全局对象: ``` (function(){ }()); ``` 第一行代码: ``` var root = typeof self == 'object' && self.self === self && self || typeof...
underscore 源码中有下面这一段代码 ``` var optimizeCb = function(func, context, argCount) { if (context === void 0) return func; switch (argCount) { case 1: return function(value) { return func.call(context, value); }; //...
underscore 源码中会将一些常用的方法缓存起来: ``` var nativeIsArray = Array.isArray, nativeKeys = Object.keys, nativeCreate = Object.create; ``` 将一些常用的原生方法缓存起来, 可以方便压缩和提高读取速度. Array.isArray 方法其实就是我们常用判断是不是数组的方法, 不过我自己常用的是 Object.prototype.toString.call() 这个方法 判断值是不是 [object Array], 如果是就是数组对象, 实际上 Array.isArray 的 polyfill 也是这么做的....