迭代器

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
// 声明一个数组
const XY = ['唐僧','孙悟空','猪八戒','沙僧'];

// 使用for...of遍历数组 遍历出是值
for(let p of XY){
console.log(p);
}
// 使用for...in遍历数组 遍历出是索引
for(let p in XY){
console.log(p);
}


// for...of工作原理
let iterator = XY[Symbol.iterator]();
// 调用 next 方法
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
</script>
</body>
</html>

posted @ 2020-08-10 20:36  Smile*^  阅读(61)  评论(0编辑  收藏  举报