[ES6] The Iterator Protocol
The iterator protocol is used to define a standard way that an object produces a sequence of values. What that really means is you now have a process for defining how an object will iterate. This is done through implementing the .next()
method.
How it Works
An object becomes an iterator when it implements the .next()
method. The .next()
method is a zero arguments function that returns an object with two properties:
value
: the data representing the next value in the sequence of values within the objectdone
: a boolean representing if the iterator is done going through the sequence of values- If done is true, then the iterator has reached the end of its sequence of values.
- If done is false, then the iterator is able to produce another value in its sequence of values.
Here’s the example from earlier, but instead we are using the array’s default iterator to step through the each value in the array.
- const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- const arrayIterator = digits[Symbol.iterator]();
- console.log(arrayIterator.next());
- console.log(arrayIterator.next());
- console.log(arrayIterator.next());
- /*
- Object {value: 0, done: false}
- Object {value: 1, done: false}
- Object {value: 2, done: false}
- */
- /*
- * Programming Quiz: Make An Iterable Object
- *
- * Turn the `james` object into an iterable object.
- *
- * Each call to iterator.next should log out an object with the following info:
- * - key: the key from the `james` object
- * - value: the value of the key from the `james` object
- * - done: true or false if there are more keys/values
- *
- * For clarification, look at the example console.logs at the bottom of the code.
- *
- * Hints:
- * - Use `Object.keys()` to store the object's properties in an array.
- * - Each call to `iterator.next()` should use this array to know which property to return.
- * - You can access the original object using `this`.
- * - To access the values of the original object, use `this` and the key from the `Object.keys()` array.
- */
- const james = {
- name: 'James',
- height: `5'10"`,
- weight: 185
- };
- james[Symbol.iterator] = function() {
- var keys = Object.keys(james);
- var nextIndex = 0;
- return {
- next: function() {
- var key = keys[nextIndex];
- var obj = {
- key: key,
- value: james[key],
- done: nextIndex === keys.length-1 ?
- true:
- false
- };
- nextIndex++;
- return obj;
- }
- }
- }
- const iterator = james[Symbol.iterator]();
- //
- console.log(iterator.next().value); // 'James'
- console.log(iterator.next().value); // `5'10`/
- console.log(iterator.next().value); //
[ES6] The Iterator Protocol的更多相关文章
- ES6的Iterator,jquery Fn
ES6的Iterator对象详解 Iterator实现原理 创建一个指针对象,指向当前数据结构的起始位置.也就是说,遍历器对象本质上,就是一个指针对象. 第一次调用指针对象的next方法,可以将指针指 ...
- Iterator Protocol - Python 描述符协议
Iterator Protocol - Python 描述符协议 先看几个有关概念, iterator 迭代器, 一个实现了无参数的 __next__ 方法, 并返回 '序列'中下一个元素,在没有更多 ...
- ES6的Iterator遍历器
JS表示集合的对象主要有Array.Set.Object.Map,在以前,遍历它们需要使用2种不同的方法,而现在,JS提出了Iterator机制,可以给不同的数据结构提供统一的遍历方法,就是for…o ...
- es6之iterator,for...of
遍历器(Iterator)是一种统一的接口机制,来处理所有不同的数据结构. JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和 ...
- JS的ES6的iterator
一.iterator 1.概念:iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制. 2.作用: 为各种数据结构,提供一个统一的.简便的访问接口: 使得数据结构的成员能够按某种次序 ...
- 前端知识点回顾之重点篇——ES6的Iterator和Generator
Iterator 迭代器是一种接口.是一种机制. 为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署 Iterator 接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). Iter ...
- ES6的 Iterator 遍历器到底是什么?
这节课要讲的是ES6中的Iterator. for...of为啥不遍历Object对象 第十三节我们讲了简单又实用的for...of,我们可以使用它来遍历数组,字符串,Set和Map结构,但是有没有发 ...
- 切图崽的自我修养-[ES6] 迭代器Iterator浅析
Iterator 这真是毅种循环 Iterator不是array,也不是set,不是map, 它不是一个实体,而是一种访问机制,是一个用来访问某个对象的接口规范,为各种不同的数据结构提供统一的访问机制 ...
- Es6 Symbol.iterator
Symbol.iterator 为每一个对象定义了默认的迭代器.该迭代器可以被 for...of 循环结构使用. --描述 当需要迭代一个对象的时候(比如在 for...of 循环的开始时),它的 @ ...
随机推荐
- 2015-8-29阿里校园招聘研发project师笔试题
前言:原题来自于网络:http://www.cnblogs.com/nausicaa/p/3946694.html.本人依据自己理解对题目进行解答.因为水平有限.题目有不会做.做错的地方.欢迎大家留言 ...
- Android 流量分析 tcpdump & wireshark
APP竞争已经白热化了,控制好自己Android应用的流量能够给用户一个良好的用户体验噢,给用户多一个不卸载的理由. Android 怎样进行流量分析?用好tcpdump & wireshar ...
- Asp.Net中使用水晶报表(中)
Asp.Net中使用水晶报表(中) 使用Pull模式 我们将通过下面的这些步骤来通过Pull模式来执行水晶报表 1.首先创建rpt文件,并使用水晶报表设计接口设置一些必须的数据连接. 2.拖放一个 C ...
- BZOJ 2127: happiness(最小割解决集合划分)
Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 2350 Solved: 1138[Submit][Status][Discuss] Descript ...
- 《剑指offer》栈的压入、弹出序列
一.题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...
- exsi主机之间使用scp拷贝文件超时问题
exsi主机之间使用scp拷贝文件直接连接不上报错超时: 解决: 防火墙勾选ssh选项
- Layout Team
The layout team is a long-term engineering team tasked with maintaining, supporting, and improving t ...
- python调用java--JPype
JPype 是一个能够让 python 代码方便地调用 Java 代码的工具,从而克服了 python 在某些领域(如服务器端编程)中的不足.JPype 的使用一个简单的 hello world 程序 ...
- NodeJS学习笔记 进阶 (11)Nodejs 进阶:调试日志打印:debug模块
个人总结:读完这篇文章需要5分钟,讲解了debug模块的使用 摘选自网络 前言 在node程序开发中时,经常需要打印调试日志.用的比较多的是debug模块,比如express框架中就用到了.下文简单举 ...
- [JSOI2007]文本生成器(AC自动机+DP)
题意 给你n个串.问有多少长度为m的串使得这n个串至少在其中出现过一次.输出答案膜10007意义下的结果. (n<=100,每个串的长度<=100) 题解 在AC自动机上跑DP. 用到一个 ...