JavaScript所有循环

while

do-while

for循环

  以上三个比较常见

for in循环

遍历对象属性及方法,以及原型属性及方法。

for( key in Obj) { }

key为键,不是值。可用Obj[key]获取

        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 of循环

for ( variable of iterable ) {  statment  }

 for...of循环调用数据接口Symbol.iterator方法(即一个数据结构部署了Symbol.iterator属性,就说明具有iterator接口,支持for...of循环)。

支持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等    

foreach

Array.prototype.forEach()

数组方法,一般用法:arr.forEach( function( vurrentValue, index,array ))

     let arr = ['jay','koo','2','3','3']
        arr.forEach((value,index)=>{
            console.log('index: ' + index + ',    value: ' + value )
        })
原文地址:https://www.cnblogs.com/jaykoo/p/10480858.html