[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 循环的开始时),它的 @ ...
随机推荐
- 一个使用sbt编译的JNI C++ 的模板
假设你须要在Scala或是Java中调用C或C++函数库,就须要使用JNI. 这里就涉及到编译scala ,java 和C(C++)代码,在这里给出一个程序的框架,我们使用sbt 缺省的代码文件夹 文 ...
- 动态语言切换(续)-designer中的retranslateUi(带源码)
本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:动态语言切换(续)-designer中 ...
- 深入理解Android之Java虚拟机Dalvik
一.背景 这个选题非常大,但并非一開始就有这么高大上的追求. 最初之时,仅仅是源于对Xposed的好奇.Xposed差点儿是定制ROM的神器软件技术架构或者说方法了. 它究竟是怎么实现呢?我本意就是想 ...
- vim 插件之vim-trailing-whitespace
vim-trailing-whitespace 这个插件是快速去掉文章行末的空格 地址 http://github.com/bronson/vim-trailing-whitespace 如果你想要使 ...
- poj--3620--Avoid The Lakes(dfs)
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
- java9新特性-14-多分辨率图像 API
1.官方Feature 251: Multi-Resolution Images 263: HiDPI Graphics on Windows and Linux 2.产生背景 在Mac上,JDK已经 ...
- ASP.NET 获取IIS应用程序池的托管管道模式
asp.net 中怎样较为简单的获取网站程序池的托管管道模式 目前已知的方式是根据这个帖子https://github.com/kakalotte/... ,利用DirectoryEntry,但是程序 ...
- Functor& Monad解读
整体上代表封装的概念,重点是函数的封装,及函数运行的上下文环境.trait Functor[F[_]] Functor:代表整体封装: F[_]:代表封装后的目标域. A.B:代表普通的对象:f:代表 ...
- Chromium Graphics: Aura
Aura (obsolete) This document is still good for a high level overview, with contact information, but ...
- centos7 初始化安装
CENTOS7 初装 一.分区 挂载路径 格式 容量 / xfs 102400 swap 等同内存大小 /home xfs 剩余 二.时区 Asia/Shanghai 三.安装包选择 选择最小化安装 ...