当前位置:网站首页>ES6 iterator explanation example
ES6 iterator explanation example
2022-08-02 03:59:00 【yorup】
Iterator
Iterator is an interface that provides a unified access mechanism for various data structures.Any data structure can complete the traversal operation as long as the lterator interface is deployed. ES6 adds the traversal method for...of.
The native data with the iterator interface are: Array, Arguments, Set, Map, String, NodeList.
The object does not have an iterator interface, but we can add one to it. The iterator is the method that comes with the data type, so add it to the object.One method is the iterator interface.
Be careful not to change the method name.Because for...of can only recognize the next() method.
Principle: Create a pointer object, pointing to the starting position of the data structure, the first time the ==next()== method is called, the pointer automatically points to the data structureThe first member, and then continue to call next(), the pointer moves backward until it points to the last member, without calling next() to return an object containing the value and done properties.
let arr = ["a","b","c"];let myIte = arr[Symbol.iterator]();//arr itself has an iterator interface, get this iterator interfaceconsole.log(myIte.next());//{value: "a", done: false}console.log(myIte.next());//{value: "b", done: false}console.log(myIte.next());//{value: "c", done: false}console.log(myIte.next());//{value: "undefined", done: true}
const Stu = {title:"web2209",persons:["Zhang San","Li Si","Wang Wu"],[Symbol.iterator](){let i=0;return {next:()=>{if(i< this.persons.length){const Obj = {value: this.persons[i], done: false};i++;return Obj;}else{return {value: undefined, done: true};}}}}}
The iterator interface method [Symbol.iterator]() is added to the object above, the following is the call, print it first
const Myite = Stu[Symbol.iterator]();console.log(Myite.next());console.log(Myite.next());console.log(Myite.next());console.log(Myite.next());
The following is to use for...of to traverse the values in the object
for(let v of Stu){console.log(v);}
边栏推荐
- [sebastian/diff] A historical change extension library for comparing two texts
- PHP有哪些杀手级超厉害框架或库或应用?
- hackmyvm: controller walkthrough
- Scrapy爬虫遇见重定向301/302问题解决方法
- GreenOptic: 1 vulnhub walkthrough
- 阿里云MySQL5.7安装以及部分主要问题(总和)
- (5) 模块与包、编码格式、文件操作、目录操作
- (2)Thinkphp6模板引擎**标签
- 批量替换文件字体,简体-&gt;繁体
- PHP realizes the automatic reverse search prompt of the search box
猜你喜欢
随机推荐
hackmyvm-random walkthrough
QR code generation API interface, which can be directly connected as an A tag
PHP 给图片添加全图水印
17.JS条件语句和循环,以及数据类型转换
MySql Advanced -- Constraints
[sebastian/diff] A historical change extension library for comparing two texts
2. PHP variables, output, EOF, conditional statements
What are the killer super powerful frameworks or libraries or applications for PHP?
Batch replace file fonts, Simplified -> Traditional
hackmyvm-hopper walkthrough
(2)Thinkphp6模板引擎**标签
hackmyvm: juggling walkthrough
c语言用栈实现计算中缀表达式
使用PHPMailer发送邮件
SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
CTF入门笔记之ping
PHP 发起支付宝支付时 订单信息乱码解决
[campo/random-user-agent]随机伪造你的User-Agent
The focus of the Dom implementation input triggers