underscore.js 分析6 map函数

作用:通过转换函数(iteratee迭代器)映射列表中的每个值产生价值的新数组。iteratee传递三个参数:value,然后是迭代 index。

_.map([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]
_.map([[1, 2], [3, 4]], _.first);
=> [1, 3]

调用过程:

1.

  // Return the results of applying the iteratee to each element.
  _.map = _.collect = function(obj, iteratee, context) {
    iteratee = cb(iteratee, context);
    var keys = !isArrayLike(obj) && _.keys(obj),
        length = (keys || obj).length,
        results = Array(length);
    for (var index = 0; index < length; index++) {
      var currentKey = keys ? keys[index] : index;
      results[index] = iteratee(obj[currentKey], currentKey, obj);
    }
    return results;
  };

2. cb函数  应该是callback的缩写。

  // An internal function to generate callbacks that can be applied to each
  // element in a collection, returning the desired result — either `identity`,
  // an arbitrary callback, a property matcher, or a property accessor.
  var cb = function(value, context, argCount) {
    if (_.iteratee !== builtinIteratee) return _.iteratee(value, context);
    if (value == null) return _.identity;
    if (_.isFunction(value)) return optimizeCb(value, context, argCount);
    if (_.isObject(value)) return _.matcher(value);
    return _.property(value);
  };

这里等于又接着调用optimizeCb,关于optimizeCb在_.each分析中有介绍,不在复述。

时间: 2024-08-29 00:23:05

underscore.js 分析6 map函数的相关文章

underscore.js中的节流函数debounce及trottle

函数节流   throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeout函数 clearTimeout(cancelTimer); cancelTimer =setTimeout(function(){ switchControl.switchAciontFactory(view, conf); },300) 代码的意思就不做多说了,实际上我无意间实现了一个debou

Underscore.js 分析

Underscore.js的源码和适合第一次看源码的人,因为文件比较小,而且没有依赖,读起来比较轻松.代码写的还很是很简练的. 我看的是1.7的源码,下面说说我觉得比较有意思的几个地方 1. _.isUndefined = function(obj) { return obj === void 0; }; 代码里好几个地方都用到了void 0,而不是undefined判断一个object是不是undefined.据说这样是因为有些浏览器允许改变undefined的值.比如undefined =

underscore.js 分析 第三天

// Create a safe reference to the Underscore object for use below. // 为Underscore对象创建一个安全的引用 // _为一个函数对象,它的实例服从单例模式. var _ = function(obj) { if (obj instanceof _) return obj; if (!(this instanceof _)) return new _(obj); this._wrapped = obj; }; 来一个最简单

js parseInt和map函数

今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了,不知道为什么答案会是[1,NaN,NaN],看了解释(http://developer.51cto.com/art/201504/474298_2.htm)但是还是没明白,没办法,只好重新无看下parseInt和map函数了. 1.parsrInt() 以前一直只有用到parseInt(x),返回一个整数,

JS中的map()函数

map()方法将调用的数组的每个元素传递给指定的函数,并返回一个数组,它包含该函数的返回值. 传递给map()的函数的调用方式和传递给forEach()的函数的调用方式一样.但传递给map()的函数应该有返回值.注意:map()返回的是新数组:它不修改调用的数组. 举个例子: 要求:为数组 arr 中的每个元素求二次方.不要直接修改数组 arr,结果返回新的数组 实现: function square(arr){ return arr.map(function(item) {  return i

underscore.js 分析 第四天

查看underscore包含多少属性和方法 通过阅读JavaScript 获取对象的键的数组 var a = _; var arr = Object.keys(a); console.log(arr); 在underscore1.9中包含了135个属性和方法.

js中的map函数

方便数组操作,返回处理后的数组 三个参数:当前元素,当前元素下标,当前map的数组 arr = [{ a: 1 }, { a: 2 }, { a: 3 }]; arr.map( (arg, arg2, arg3) => { console.log(arg,arg2,arg3);return arg['a']}) 原文地址:https://www.cnblogs.com/guofan/p/9579686.html

underscore.js源码解析(二)

前几天我对underscore.js的整体结构做了分析,今天我将针对underscore封装的方法进行具体的分析,代码的一些解释都写在了注释里,那么废话不多说进入今天的正文. 没看过上一篇的可以猛戳这里:underscore.js源码解析(一) underscore.js源码GitHub地址: https://github.com/jashkenas/underscore/blob/master/underscore.js 本文解析的underscore.js版本是1.8.3 _.each 1

浅谈 Underscore.js 中 _.throttle 和 _.debounce 的差异

Underscore.js是一个很精干的库,压缩后只有5.2KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程. 本文仅探讨Underscore.js的两个函数方法 _.throttle 和 _.debounce 的原理.效果和用途. 通常的函数(或方法)调用过程分为三个部分:请求.执行和响应.(文中“请求”与“调用”同义,“响应”与“返回”同义,为了更好的表述,刻意采用请求和响应的说法.) 某些场景下,比如响应鼠标移动或者窗口大小调整的事件,触发频率