zoukankan      html  css  js  c++  java
  • 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 )
            })
  • 相关阅读:
    [SUCTF 2019]Pythonginx
    Buuctf-RSA1
    [网鼎杯 2020 朱雀组]phpweb
    [BJDCTF2020]ZJCTF,不过如此
    CTFHub-信息泄露
    vaex读取和处理大型文件的方法
    "生猪数据统计分析系统"----帮助文档
    法治理论1
    很短暂,很喜欢,很遗憾
    「CF1513E Cost Equilibrium」
  • 原文地址:https://www.cnblogs.com/jaykoo/p/10480858.html
Copyright © 2011-2022 走看看