当前位置:网站首页>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);}
边栏推荐
- IO streams, byte stream and byte stream buffer
- hackmyvm: juggling walkthrough
- [phpunit/php-timer] A timer for code execution time
- Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
- PHP的几个有趣的打开方式:从基本到变态
- 百度定位js API
- PHP有哪些杀手级超厉害框架或库或应用?
- Smart Tips for Frida Scripting in Kali Environment
- Masashi: 1 vulnhub walkthrough
- 逍遥多开模拟器ADB驱动连接
猜你喜欢

PHP8.2中字符串变量解析的新用法

kali安装IDEA

Kali install IDEA

SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration

(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合

New usage of string variable parsing in PHP8.2

PHP Foundation March Press Announcement Released

Kali环境下Frida编写脚本智能提示

hackmyvm-bunny预排

MySql Advanced -- Constraints
随机推荐
2.PHP变量、输出、EOF、条件语句
Praying: 1 vulnhub walkthrough
(6) 学生信息管理系统设计
PHP入门(自学笔记)
批量替换文件字体,简体-&gt;繁体
Eric靶机渗透测试通关全教程
16.JS事件, 字符串和运算符
二维码生成API接口,可以直接作为A标签连接
VIKINGS: 1 vulnhub walkthrough
What will be new in PHP8.2?
Baidu positioning js API
[campo/random-user-agent] Randomly fake your User-Agent
(5) Modules and packages, encoding formats, file operations, directory operations
hackmyvm: again walkthrough
(8) requests、os、sys、re、_thread
14.JS语句和注释,变量和数据类型
JS objects, functions and scopes
GreenOptic: 1 vulnhub walkthrough
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
(4) 函数、Bug、类与对象、封装、继承、多态、拷贝