angular forEach方法遍歷源碼解讀
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);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- angular.foreach 循環(huán)方法使用指南
- AngularJS ng-repeat指令中使用track by子語句解決重復(fù)數(shù)據(jù)遍歷錯(cuò)誤問題
- Angular ng-repeat遍歷渲染完頁面后執(zhí)行其他操作詳細(xì)介紹
- Angular ng-repeat 對(duì)象和數(shù)組遍歷實(shí)例
- AngularJS入門(用ng-repeat指令實(shí)現(xiàn)循環(huán)輸出
- angular.element方法匯總
- angularJS中$apply()方法詳解
- 使用AngularJS來實(shí)現(xiàn)HTML頁面嵌套的方法
- angularjs 處理多個(gè)異步請(qǐng)求方法匯總
- 解決angular的$http.post()提交數(shù)據(jù)時(shí)后臺(tái)接收不到參數(shù)值問題的方法
相關(guān)文章
對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別
這篇文章主要給大家對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別,十分的詳細(xì),是篇非常不錯(cuò)的文章,這里推薦給小伙伴們。2015-02-02
詳解angularJs中自定義directive的數(shù)據(jù)交互
這篇文章主要介紹了詳解angularJs中自定義directive的數(shù)據(jù)交互,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
AngularJS 實(shí)現(xiàn)按需異步加載實(shí)例代碼
這篇文章主要介紹了AngularJS 實(shí)現(xiàn)按需異步加載實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2015-10-10
AngularJS雙向數(shù)據(jù)綁定原理之$watch、$apply和$digest的應(yīng)用
這篇文章主要介紹了AngularJS雙向數(shù)據(jù)綁定原理之$watch、$apply和$digest的應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
AngularJS入門教程之?dāng)?shù)據(jù)綁定原理詳解
這篇文章主要介紹了AngularJS數(shù)據(jù)綁定原理,較為詳細(xì)的分析了AngularJS數(shù)據(jù)綁定的原理、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-11-11
SpringMVC簡(jiǎn)單整合Angular2的示例
這篇文章主要介紹了SpringMVC簡(jiǎn)單整合Angular2的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
angular4響應(yīng)式表單與校驗(yàn)實(shí)現(xiàn)demo
這篇文章主要介紹了angular4響應(yīng)式表單與校驗(yàn)實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

