发布于 2017-05-28 23:06:56 | 234 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的AngularJS开发指南,程序狗速度看过来!

AngularJS 前端JS框架

AngularJS诞生于Google是一款优秀的前端JS框架,已经被用于Google的多款产品当中。AngularJS有着诸多特性,最为核心的是:MVC、模块化、自动化双向数据绑定、语义化标签、依赖注入,等等。


这篇文章主要为大家详细了angular forEach方法遍历源码,forEach()方法用于遍历对象或数组,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

angular中提供了forEach()方法用于遍历对象或数组,供大家参考,具体内容如下


function forEach(obj, iterator, context) {
 var key, length;
 if (obj) {
  if (isFunction(obj)) {
   for (key in obj) {
    // 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);
    }
   }
  } else if (isArray(obj) || isArrayLike(obj)) {
   var isPrimitive = typeof obj !== 'object';
   for (key = 0, length = obj.length; key < length; key++) {
    if (isPrimitive || key in obj) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  } else if (obj.forEach && obj.forEach !== forEach) {
    obj.forEach(iterator, context, obj);
  } else if (isBlankObject(obj)) {
   // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
   for (key in obj) {
    iterator.call(context, obj[key], key, obj);
   }
  } else if (typeof obj.hasOwnProperty === 'function') {
   // Slow path for objects inheriting Object.prototype, hasOwnProperty check needed
   for (key in obj) {
    if (obj.hasOwnProperty(key)) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  } else {
   // Slow path for objects which do not have a method `hasOwnProperty`
   for (key in obj) {
    if (hasOwnProperty.call(obj, key)) {
     iterator.call(context, obj[key], key, obj);
    }
   }
  }
 }
 return obj;
}

官方描述:

forEach方法可以遍历数组或对象,函数有三个参数为别为:value,key,obj。
1)、value value指当遍历的对象或数组元素当前的值
2)、 key 是对象属性的的key或者数组的索引
3)、 obj obj即被遍历的对象或数组本身

示例:


   var values = {name: 'misko', gender: 'male'};
   var log = [];
   angular.forEach(values, function(value, key) {
    this.push(key + ': ' + value);
   }, log);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持phperz。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务