以上三個比較常見數組
遍歷對象屬性及方法,以及原型屬性及方法。數據結構
for( key in Obj) { }this
key爲鍵,不是值。可用Obj[key]獲取spa
let Father = function(){ this.o= 'ooo', this.k= 'kkk' } let Child = function(){ this.n= 'n', this.o= 'o', this.t= 't', this.method = function(){ console.log('233') } } Child.prototype = new Father() let child = new Child() for(key in child){ console.log(key + ': ' + child[key]) } —————————————————————————————————————————————————————————— // 輸出 /* n: n o: o t: t method: function(){ console.log('233') } k: kkk */
for ( variable of iterable ) { statment }prototype
for...of循環調用數據接口Symbol.iterator方法(即一個數據結構部署了Symbol.iterator屬性,就說明具備iterator接口,支持for...of循環)。code
支持Array、Set、Map,相似數組的對象(arguments、NodeList)、Generator、字符串(String)對象
// Array:1,2,3 for(let a of [1,2,3]){ console.log(a) } // Set: 2,3,4 for(let s of new Set([2,3,3,4])){ console.log(s) } // Map: name:jay, // weight:233 let map = new Map().set('name','jay').set('weight',233) for(let [key,value] of map){ console.log(key + ':' + value) } // String: s,t,r,i,n,g for(let s of 'string'){ console.log(s) } // Generator // 支持NodeList和arguments等
數組方法,通常用法:arr.forEach( function( vurrentValue, index,array ))接口
let arr = ['jay','koo','2','3','3'] arr.forEach((value,index)=>{ console.log('index: ' + index + ', value: ' + value ) })