YUI Array 之each| forEach(遍历)
1. yui-each原码:

遍历YArray.each = YArray.forEach = Lang._isNative(Native.forEach) ? function (array, fn, thisObj) {
Native.forEach.call(array || [], fn, thisObj || Y);
return Y;
} : function (array, fn, thisObj) {
for (var i = 0, len = (array && array.length) || 0; i < len; ++i) {
if (i in array) {
fn.call(thisObj || Y, array[i], i, array);
}
}
return Y; //返回Y,便于链式操作
};
2.tangram-each

tangram遍历var T,baidu=T= function(){
///import baidu;
baidu.each = function( enumerable, iterator, context ) {
var i, n, t, result;
if ( typeof iterator == "function" && enumerable) {
// Array or ArrayLike or NodeList or String or ArrayBuffer
n = typeof enumerable.length == "number" ? enumerable.length : enumerable.byteLength;
if ( typeof n == "number" ) {
// 20121030 function.length
//safari5.1.7 can not use typeof to check nodeList - linlingyu
if (Object.prototype.toString.call(enumerable) === "[object Function]") {
return enumerable;
}
for ( i=0; i<n; i++ ) {
//enumerable[ i ] 有可能会是0
t = enumerable[ i ];
t === undefined && (t = enumerable.charAt && enumerable.charAt( i ));
// 被循环执行的函数,默认会传入三个参数(i, array[i], array)
result = iterator.call( context || t, i, t, enumerable );
// 被循环执行的函数的返回值若为 false 和"break"时可以影响each方法的流程
if ( result === false || result == "break" ) {break;}
}
// enumerable is number
} else if (typeof enumerable == "number") {
for (i=0; i<enumerable; i++) {
result = iterator.call( context || i, i, i, i);
if ( result === false || result == "break" ) { break;}
}
// enumerable is json
} else if (typeof enumerable == "object") {
for (i in enumerable) {
if ( enumerable.hasOwnProperty(i) ) {
result = iterator.call( context || enumerable[ i ], i, enumerable[ i ], enumerable );
if ( result === false || result == "break" ) { break;}
}
}
}
}
return enumerable;
};
return baidu;
}();
3.tangram-array each原码

tangram array遍历var T,baidu=T= function(){
///import baidu.type;
///import baidu.array;
///import baidu.each;
///import baidu.forEach;
baidu.array.extend({
each: function(iterator, context){
return baidu.each(this, iterator, context);
},
forEach: function(iterator, context){
return baidu.forEach(this, iterator, context);
}
});
/// Tangram 1.x Code Start
// TODO: delete in tangram 3.0
baidu.array.each = baidu.array.forEach = function(array, iterator, context) {
var fn = function(index, item, array){
return iterator.call(context || array, item, index, array);
};
return baidu.isEnumerable(array) ? baidu.each(array, typeof iterator == "function" ? fn : "", context) : array;
};
/// Tangram 1.x Code End
return baidu;
}();
4.underscore each 原码:

underscore array遍历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 (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;
}
}
}
};
5. qwrap – each

qwrap array遍历forEach: function(arr, callback, pThis) {
for (var i = 0, len = arr.length; i < len; i++) {
if (i in arr) {
callback.call(pThis, arr[i], i, arr);
}
}
}
比较
a. YUI会调用原生的forEach,可以用来遍历array,string,不能遍历object,也不会打断遍历
b.tangram最严谨,排除了function具有length属性,但YUI与underscore用了in 这个来排除掉了function,可以返回break 或者false打断遍历,可以用来遍历对象
c. underscore 会调用原生的forEach,可以用来遍历对象,数组,字符串,可以被打断遍历,这个函数在underscore是一个基础的函数,
d. qwrap的最简洁,不会调用原生forEach,也不能打断遍历
YUI Array 之each| forEach(遍历)的更多相关文章
- 了解PHP中的Array数组和foreach
1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组 . 2.例子:一般的数组 这里,我 ...
- 实现Foreach遍历
实现Foreach遍历的集合类,需要实现IEnumerable接口,泛型集合则需要实现IEnumerable<T>接口 using System; using System.Collect ...
- c#--foreach遍历的用法与split的用法
一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素. 该循环 ...
- thinkphp使用foreach遍历的方法
我们在做一些需求的时候可能会对遍历的上限有一定的要求,这时候就需要对上限进行限定 首先使用foreach遍历的输出数组相比较于volist功能较少 volist标签主要用于在模板中循环输出数据集或者多 ...
- 用数组指针遍历数组,FOR/FOREACH遍历数组
1. 用数组指针遍历一维数组 <?php header("Content-type:text/html;charset=utf-8"); /*用数组指针遍历一位数组的值*/ ...
- forEach遍历数组对象且去重
forEach遍历数组对象 var obj1 = [{ key: '01', value: '哈哈' }, { key: '02', value: '旺旺' }, { key: '03', value ...
- 为什么iterator,foreach遍历时不能进行remove操作?除了一种情况可以这样(特殊情况)?
Exception in thread "main" java.util.ConcurrentModificationException 并发修改异常引发的思考! 1 foreac ...
- foreach遍历数组
foreach遍历一维数组 <?php //PHP数组遍历:foreach //定义数组 $arr=array(1,2,3,4,5,6,7,8,9,10); //foreach循环 foreac ...
- PHP foreach遍历数组之如何判断当前值已经是数组的最后一个
先给出foreach的两种语法格式 1,foreach (array_expression as $value) statement 2,foreach (array_expression as $k ...
随机推荐
- C# ITextSharp pdf 自动打印
PDF生成后直接进入打印预览不用下载 using iTextSharp.text; using iTextSharp.text.pdf; Document pdfDoc = new Document( ...
- 设置Toast显示位置
设置Toast显示位置 两个方法可以设置显示位置: 方法一:setGravity(int gravity, int xOffset, int yOffset)三个参数分别表示(起点位置,水平向右位移, ...
- IOS 文件管理 2
IOS开发-文件管理(二) 五.Plist文件 String方式添加 NSString *path = [NSHomeDirectory( ) stringByAppen ...
- LeetCode_Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 分析统计<第三篇>
统计是一组存储为柱状图的信息.柱状图是显示数据落入不通分类中的频率的一种统计结构.SQL Server存储的柱状图包括多大200行的列和索引键(或多列索引键的第一列)的数据分布采样.在两个连续采样值之 ...
- 链接一个外部lib库的时候注意事项
1.注意这个库是Debug版还是Release版,一般windows下,约定是Debug版的库文件名会加个d. 2.注意这个库是x86还是x64版本. 3.注意生成这个lib库的是什么编译器
- Square spiral
Square spiral Nikola picks up a strange circuit board. All of its elements are connected in a spiral ...
- 自己实现的简单MVC框架(类似Struts2+Spring)
一.框架简介 本框架是一个类似于Struts2+Spring的框架,目的在于个人钻研和技术分享,将流行技术框架Struts2.Spring中使用到的主要技术以较为简化的方式实现出来,给大家一个更直观的 ...
- Base64 加密之中文乱码
ase64编码将二进制数据按照每三个字节转换成四个字节可读字符,编码后的字符长度大约为136.1%.字符范围为 A-Z a-z 0-9 \ +.但编码后的字符串不太适合使用URL传输,中文加密 ...
- Hdu5126-stars(两次CDQ分治)
题意: 简化就是有两种操作,一种是插入(x,y,z)这个坐标,第二种是查询(x1,y1,z1)到(x2,y2,z2)(x1<=x2,y1<=y2,z1<=z2)的长方体包含多少个点. ...