js中for in 与 for of

转载自:https://segmentfault.com/q/1010000006658882

 

简单说,for in是遍历键名,for of是遍历键值。例如:

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

for (a of arr) {
    console.log(a);//a,b
}

由于for of的这个特性,他还可以实现对iterator对象的遍历,而for in就是简单的遍历了。

posted @ 2017-06-18 08:15  hasic  阅读(251)  评论(1)    收藏  举报