当前位置:网站首页>ES6 迭代器与生成器
ES6 迭代器与生成器
2022-08-11 05:17:00 【前端小马】
什么是迭代:
从一个数据集合中按照一定的顺序,不断的取出数据的过程(很容易就想到遍历)。
迭代器是一个带有特殊接口(Iterator )的对象,自己带有next()方法,next ()方法会返回当前位置的对象,调用后得到一个包含value和done属性的对象,value属性表示当前属性的值,done用于判断是否遍历结束,当 done 为 true 时遍历结束。。任何数据结构只要部署 Iterator 接口,就可以完成遍历操作
ES6 创造了一种新的遍历命令 for...of 循环,Iterator 接口主要供 for...of 使用
ES6规定,如果一个对象具有属性
Symbol.iterator
,并且属性值是一个迭代器创建函数,则该对象是可迭代的(iterable);
原生具备 iterator 接口的数据(可用 for of 遍历):
1.Array 2. Arguments 3.Set 4. Map 5.String 6.TypedArray 7.NodeList
迭代和遍历的区别:
迭代强调的是依次取数据,并不保证取多少,也不保证把所有的数据取完
遍历强调的是要把整个数据依次全部取出
工作原理 :
1. 通过 Symbol.iterator 创建一个指针对象,指向当前数据结构的起始位置。
2. 第一次调用对象的 next 方法,指针自动指向数据结构的第一个成员
3. 接下来不断调用 next 方法,指针一直往后移动,直到指向最后一个成员
4. 每调用 next 方法返回一个包含 value 和 done 属性的对象
var num=[1,2,3,4,5]
for(var i of num){
console.log(i) //1 2 3 4
}
var iterator=num[Symbol.iterator]() //通过 Symbol.iterator 创建一个指针对象
console.log(iterator.next()) //{value: 1, done: false}
console.log(iterator.next()) //{value: 2, done: false}
console.log(iterator.next()) //{value: 3, done: false}
console.log(iterator.next()) //{value: 4, done: false}
console.log(iterator.next()) //{value: undefined, done: true}
生成器:
生成器既是一个迭代器,同时又是一个可迭代对象
生成器函数内部是为了给生成器的每次迭代提供的数据
每次调用生成器的next方法,将导致生成器函数运行到下一个yield关键字位置
yield是一个关键字,该关键字只能在生成器函数内部使用,表达“产生”一个迭代数据。
function * fun(){
console.log(1)
}
fun() //函数不执行
var iterator=fun()
iterator.next() //必须调用.next()函数才会执行
function * fun(){
yield "1"
yield "2"
yield "3"
}
//因为是迭代器对象所以可以用for of,每次调用的结果是yield后面写的值
for(var i of fun()){
console.log(i) //分别输出1,2,3
}
function * fun(){
yield "1" //yield可以算是函数代码分隔符,每调用一次next()就执行一行
yield "2"
yield "3"
}
var iterator=fun()
//生成器函数返回的结果是迭代器对象,调用迭代器对象的 next 方法可以得到yield 语句后的值
console.log(iterator.next()) //{value: '1', done: false}
console.log(iterator.next()) //{value: '2', done: false}
console.log(iterator.next()) //{value: '3', done: false}
console.log(iterator.next()) //{value: undefined, done: true}
function * fun(){
var a= yield "1"
console.log(a) //7
var b= yield "2"
console.log(b) //8
var c= yield "3"
console.log(c) //9
}
var iterator=fun()
iterator.next(6)//next()方法可以传入实参 第一次调用next方法时,传参没有任何意义
iterator.next(7) //从第二次调用next()开始,传入的实参依次作为yield语句的返回结果
iterator.next(8)
iterator.next(9)
生成器应用(异步任务):
一秒后输出1 二秒后输出2 三秒后输出3
setTimeout(()=>{
console.log(1)
setTimeout(()=>{
console.log(2)
setTimeout(()=>{
console.log(3)
},3000)
},2000)
},1000) //如果嵌套太多就形成回调地狱
---------------------------------------------------
//可以使用生成器解决
function one(){
setTimeout(()=>{
console.log(1)
iterator.next() //调用next
},1000)
}
function two(){
setTimeout(()=>{
console.log(2)
iterator.next() //调用next
},2000)
}
function three(){
setTimeout(()=>{
console.log(3)
},3000)
}
function* fun(){
yield one()
yield two()
yield three()
}
var iterator=fun() //调用生成器函数
iterator.next()
边栏推荐
- bootstarp作业一:制作分页器
- 【记录】innerHeight?clientHeight?offsetHeight?scrollTop?screenTop?.....一堆高度傻傻分不清
- 【转载】CMake 语法 - 详解 CMakeLists.txt
- c 指针学习(1)
- C语言——文件操作详解(1)
- 表单input控件数据双向绑定
- Who am I ?
- npm install 时报 npm ERR Cannot read properties of null (reading ‘pickAlgorithm‘)
- 吃瓜教程task04 第5章 神经网络
- Chapter 13 Class Inheritance
猜你喜欢
C - file operations fseek () function, ftell, rewind, rounding
(3) How Redis performs stress testing
07-JS事件:事件类型、事件对象、事件传播、事件委托
task05 PyTorch可视化
task06 PyTorch生态
Django--20 implements Redis support, context, and interaction of context and interface
【win10+cuda7.5+cudnn6.0安装caffe④】安装pycaffe
vscode插件
task04 Pytorch进阶训练技巧
RK3399上的Tengine实践笔记
随机推荐
QT QLabel控件(使用详解)
Django--20 implements Redis support, context, and interaction of context and interface
【win10+cuda7.5+cudnn6.0安装caffe①】安装cuda和cudnn
[转载]Verilog testbench总结
(2) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Jmeter)
C语言之EOF、feof函数、ferror函数
【网站小白】mySQL数据库异常断开
博客帮助文档
第13章 类的继承-1
Error in render: “TypeError: Cannot read properties of undefined (reading ‘kingKongList‘)“
[C language from elementary to advanced] Part 2 Initial C language (2)
C language - program compilation and execution, detailed macro definition
C语言——逆序输出字符串的函数实现
字符与字符串
Chapter 5 Loops and Relational Expressions
【记录】innerHeight?clientHeight?offsetHeight?scrollTop?screenTop?.....一堆高度傻傻分不清
看完这篇博客之后,含着泪学会了TCP/IP
QT GrabWindow截取屏幕
LeetCode43. String multiplication (this method can be used to multiply large numbers)
CSDN 社区内容创作规范