Inerator接口和for...of循环

一、Inerator接口:

  有三类数据结构具有原生的Inerator接口,数组、某些类似数组的对象(如字符串)、以及Set和Map结构。其他类型的数据结构,只能通过部署inerator接口(如对象),实现遍历。

 

二、for...of:

  本质上是调用Inerator接口而产生的遍历器,对于JS原有的for...in方法,for...of方法可以获得键值,而for...in方法只能获得键名,例如:

var arr = ["a","b","c"];
for(let x in arr){
    console.log(x);      // 0 1 2
}
for(let x of arr){
    console.log(x);      // a b c
}

   获取[key,value]类型的数组,for...of也可以选择性获取性获取该数组的key值或者value值

posted @ 2017-11-15 16:50  Shmily-HJT  阅读(132)  评论(0)    收藏  举报