博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
underscore.js _.each[Collections]
阅读量:4313 次
发布时间:2019-06-06

本文共 1211 字,大约阅读时间需要 4 分钟。

Iterates over a list of elements, yielding each in turn to an iterator function. Theiterator is bound to the context object, if one is passed. Each invocation of iteratoris called with three arguments: (element, index, list). If list is a JavaScript object, iterator's arguments will be (value, key, list). Delegates to the nativeforEach function if it exists.

遍历函数原色,产生一个迭代的方法。 别名forEach .可遍历数组和对象。

 

1 _.each([1, 2, 3], function(num){ alert(num); });2 => alerts each number in turn...3 _.each({one : 1, two : 2, three : 3}, function(num, key){ alert(num); });4 => alerts each number in turn...

 

源码

var each = _.each = _.forEach = function(obj, iterator, context) {    if (obj == null) return;    if (nativeForEach && obj.forEach === nativeForEach) {      obj.forEach(iterator, context);    } else if (obj.length === +obj.length) {      for (var i = 0, l = obj.length; i < l; i++) {        if (i in obj && iterator.call(context, obj[i], i, obj) === breaker) return;      }    } else {      for (var key in obj) {        if (_.has(obj, key)) {          if (iterator.call(context, obj[key], key, obj) === breaker) return;        }      }    }  };

  

 

 

 

转载于:https://www.cnblogs.com/himan/archive/2012/04/20/2460661.html

你可能感兴趣的文章
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>
Docker面试题(二)
查看>>
【NOI 2018】归程(Kruskal重构树)
查看>>
注册用户
查看>>
TZC Intercommunication System
查看>>
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>