Iterator 是什么
// 1.Iterator 的作用// Iterator:遍历器(迭代器)// for()// [1,2].forEach// new Set().forEach// Iterator 也是用来遍历的// 2.寻找 Iterator// console.log(Iterator);console.log([1, 2][Symbol.iterator]());const it = [1, 2][Symbol.iterator]();console.log(it);// 3.使用 Iterator// const it = [1, 2][Symbol.iterator]();// console.log(it.next()); // {value: 1, done: false}// console.log(it.next()); // {value: 2, done: false}// console.log(it.next()); // {value: undefined, done: true}// console.log(it.next()); // {value: undefined, done: true}// it:可遍历对象(可迭代对象)// Symbol.iterator:可遍历对象的生成方法// 4.什么是 Iterator// Symbol.iterator(可遍历对象的生成方法) -> it(可遍历对象) -> it.next() -> it.next() -> ...(直到 done 为 true)
Iterator 解惑
// 1.为什么需要 Iterator 遍历器// 遍历数组:for 循环和 forEach 方法// 遍历对象:for in 循环// Iterator 遍历器是一个统一的遍历方式// console.log([][Symbol.iterator]());// console.log({}[Symbol.iterator]);// 2.如何更方便的使用 Iterator// Symbol.iterator->it->next()// 我们一般不会直接使用 Iterator 去遍历// for..of
for…of 的用法
// 1.认识 for...of// const arr = [1, 2, 3];// // const it = arr[Symbol.iterator]();// // // console.log(it.next());// // // console.log(it.next());// // // console.log(it.next());// // // console.log(it.next());// // let next = it.next();// // console.log(next);// // while (!next.done) {// // console.log(next.value);// // next = it.next();// // console.log(next);// // }// for (const item of arr) {// console.log(item);// }// // for...of 循环只会遍历出那些 done 为 false 时,对应的 value 值// 2.与 break、continue 一起使用// const arr = [1, 2, 3];// for (const item of arr) {// if (item === 2) {// // break;// continue;// }// console.log(item);// }// arr.forEach()// 3.在 for...of 中取得数组的索引const arr = [1, 2, 3];// keys() 得到的是索引的可遍历对象,可以遍历出索引值console.log(arr.keys());// for (const key of arr.keys()) {// // console.log(key);// }// // values() 得到的是值的可遍历对象,可以遍历出值// for (const value of arr.values()) {// console.log(value);// }// for (const value of arr) {// console.log(value);// }// entries() 得到的是索引+值组成的数组的可遍历对象// for (const entries of arr.entries()) {// console.log(entries);// }for (const [index, value] of arr.entries()) {console.log(index, value);}
原生可遍历和非原生可遍历
// 1.什么是可遍历// 只要有 Symbol.iterator 方法,并且这个方法可以生成可遍历对象,就是可遍历的// 只要可遍历,就可以使用 for...of 循环来统一遍历// 2.原生可遍历的有哪些// 数组// 字符串// Set// Map// arguments// NodeList// for (const item of [1, 2, 3]) {// console.log(item);// }// for (const item of 'hi') {// console.log(item);// }// for (const item of new Set([1, 2])) {// console.log(item);// }// for (const elem of document.querySelectorAll('p')) {// console.log(elem);// elem.style.color = 'red';// }// 3.非原生可遍历的有哪些// 一般的对象const person = { sex: 'male', age: 18 };// console.log(person[Symbol.iterator]());// {next()} {value,done}// person[Symbol.iterator] = () => {// let index = 0;// return {// next() {// index++;// if (index === 1) {// return {// value: person.age,// done: false// };// } else if (index === 2) {// return {// value: person.sex,// done: false// };// } else {// return {// done: true// };// }// }// };// };// for (const item of person) {// console.log(item);// }// for in// 有 length 和索引属性的对象const obj = {'0': 'alex','1': 'male',length: 2};obj[Symbol.iterator] = Array.prototype[Symbol.iterator];// obj[Symbol.iterator] = () => {// let index = 0;// return {// next() {// let value, done;// if (index < obj.length) {// value = obj[index];// done = false;// } else {// value = undefined;// done = true;// }// index++;// return {// value,// done// };// }// };// };for (const item of obj) {console.log(item);}
使用了 Iterator 的场合
// 原生可遍历的// Array 数组// String 字符串// Set// Map// 函数的 arguments 对象// NodeList 对象// for...of// 1.数组的展开运算符// console.log(...[1, 2, 3]);// console.log(1, 2, 3);// console.log(...'str');// console.log(...new Set([1, 2, 3]));// console.log(...{}); ×// 2.数组的解构赋值// const [a, b] = [1, 2];// const [a, b] = [...[1, 2]];// const [a, b] = 'hi';// const [a, b] = [...'hi'];// const [a, b] = [...new Set([3, 4])];// console.log(a, b);// 3.Set 和 Map 的构造函数// new Set(iterator)// new Map(iterator)
