加载中...

for...of

  • 内部的原理就是iterator

  • for(const item of arr){
    console.log(item)
    } //for..of只会遍历出done为false的值

  • 回顾:break跳出当前循环,continue跳出当次循环

  • 获取索引值
    arr=[1,2,3]
    for(const key of arr.keys()){
    console.log(key)
    } //0,1,2
    for(const [index,value] of arr.entries()){
    console.log(index,value)
    }//0,1 1,2 2,3

  • 原生可遍历:数组,字符串,Set, Map, arguments, NodeList(这些都可以直接使用for...of)

  • 非原生可遍历:

posted @ 2021-10-26 16:43  莫等闲O(∩_∩)O~~  阅读(50)  评论(0)    收藏  举报