欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angular forEach方法遍歷源碼解讀

 更新時(shí)間:2017年01月25日 09:33:38   作者:Sophie_U  
這篇文章主要為大家詳細(xì)了angular forEach方法遍歷源碼,forEach()方法用于遍歷對(duì)象或數(shù)組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

angular中提供了forEach()方法用于遍歷對(duì)象或數(shù)組,供大家參考,具體內(nèi)容如下

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方法可以遍歷數(shù)組或?qū)ο?,函?shù)有三個(gè)參數(shù)為別為:value,key,obj。
1)、value value指當(dāng)遍歷的對(duì)象或數(shù)組元素當(dāng)前的值
2)、 key 是對(duì)象屬性的的key或者數(shù)組的索引
3)、 obj obj即被遍歷的對(duì)象或數(shù)組本身

示例:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論