For of
for of 可以遍历哪些类型的数据
-
Array
-
字符串
-
nodeList
-
类数组对象
-
arguments
-
Map
-
Set
例:
//遍历字符串
for(let val of 'abcdef'){
console.log(val);
}
//nodeList
var lis =document.querySelectorAll('ul li');
for (let ele of lis){
console.log(ele);
}
输出:

for of 与 其他遍历方式做个比较
-
for..in : 可以遍历的类型比for in 更多, for of直接遍历出值, for in便利出来的是key
-
forEach方式:可以遍历更多的类型, 可以使用 break 和 continue等结束语句
Iterator 遍历器
-
ES6定义的一个接口
-
Array实现了Interator的接口
-
实现遍历器接口的数据类型都可以用于 for of
本文由 倪建成学习时 编辑,只是普通笔记。若是对你有用,可以自行转载
最后编辑时间为:2019-08-29 00:00:00