最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
angular forEach方法遍历源码解读
时间:2017-03-07 编辑:简简单单 来源:一聚教程网
代码如下 | 复制代码 |
functionforEach(obj, iterator, context) { varkey, length; if(obj) { if(isFunction(obj)) { for(keyinobj) { // Need to check if hasOwnProperty exists, // as on IE8 the result of querySelectorAll is an object without a hasOwnProperty function if(key !='prototype'&& key !='length'&& key !='name'&& (!obj.hasOwnProperty || obj.hasOwnProperty(key))) { iterator.call(context, obj[key], key, obj); } } }elseif(isArray(obj) || isArrayLike(obj)) { varisPrimitive =typeofobj !=='object' for(key = 0, length = obj.length; key < length; key++) { if(isPrimitive || keyinobj) { iterator.call(context, obj[key], key, obj); } } }elseif(obj.forEach && obj.forEach !== forEach) { obj.forEach(iterator, context, obj); }elseif(isBlankObject(obj)) { // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty for(keyinobj) { iterator.call(context, obj[key], key, obj); } }elseif(typeofobj.hasOwnProperty ==='function') { // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed for(keyinobj) { if(obj.hasOwnProperty(key)) { iterator.call(context, obj[key], key, obj); } } }else{ // Slow path for objects which do not have a method `hasOwnProperty` for(keyinobj) { if(hasOwnProperty.call(obj, key)) { iterator.call(context, obj[key], key, obj); } } } } returnobj; } |
官方描述:
forEach方法可以遍历数组或对象,函数有三个参数为别为:value,key,obj。
1)、value value指当遍历的对象或数组元素当前的值
2)、 key 是对象属性的的key或者数组的索引
3)、 obj obj即被遍历的对象或数组本身
示例:
代码如下 | 复制代码 |
varvalues = {name:'misko', gender:'male'}; varlog = []; angular.forEach(values,function(value, key) { this.push(key +': '+ value); }, log); |
-
上一个: three.js实现围绕某物体旋转
-
下一个: jquery实现拖动效果的代码分享
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31