当前位置:网站首页>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);}
边栏推荐
- php函数漏洞总结
- 4. The form with the input
- 16. JS events, string and operator
- 3. PHP data types, constants, strings and operators
- 1.初识PHP
- When PHP initiates Alipay payment, the order information is garbled and solved
- DVWA drone installation tutorial
- hackmyvm: may walkthrough
- What are the PHP framework?
- [league/flysystem] An elegant and highly supported file operation interface
猜你喜欢
CTF入门之php文件包含
Smart Tips for Frida Scripting in Kali Environment
(3)Thinkphp6数据库
解决 Zlibrary 卡死/找不到域名/达到限额问题,Zlibrary最新地址
The roll call system and array elements find maximum and minimum values for sorting of objects
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
(7) superficial "crawlers" process (concept + practice)
[league/climate] A robust command-line function manipulation library
hackmyvm-random walkthrough
(4) Function, Bug, Class and Object, Encapsulation, Inheritance, Polymorphism, Copy
随机推荐
PHP有哪些框架?
Add a full image watermark to an image in PHP
CTF-网鼎杯往届题目
Turn trendsoft/capital amount of Chinese capital library
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
查询数据库中所有表的索引,并且解析成sql
(2)Thinkphp6模板引擎**标签
Several interesting ways to open PHP: from basic to perverted
MOMENTUM: 2 vulnhub walkthrough
Alfa: 1 vulnhub walkthrough
(7) superficial "crawlers" process (concept + practice)
[phpunit/php-timer] A timer for code execution time
PHP图片压缩到指定的大小
CTF入门笔记之ping
二维码生成API接口,可以直接作为A标签连接
(8) requests、os、sys、re、_thread
(7) 浅学 “爬虫” 过程 (概念+练习)
Basic use of v-on, parameter passing, modifiers
hackmyvm: again walkthrough
攻防世界—MISC 新手区1-12