Inerator
两个核心 是一种新的遍历机制
- 迭代器是一个接口,能快捷的访问数据,通过
Symbol.iterator
创建迭代器,通过迭代器的next方法获取迭代之后的结果 - 迭代器是用于遍历数据结构的指针(数据库的游标)
使用迭代:
const items = ['one','two','three'];
const ite = items[Symbol.iterator]();
console.log(ite);//Array Iterator {}
console.log(ite.next())//{value: "one", done: false}
console.log(ite.next())//{value: "two", done: false}
console.log(ite.next())//{value: "three", done: false}//done为false表示遍历继续,为true表示遍历完成
console.log(ite.next())//{value: undefined, done: true}
网友评论