博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[ES6] The Iterator Protocol
阅读量:4347 次
发布时间:2019-06-07

本文共 2702 字,大约阅读时间需要 9 分钟。

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:

  1. value : the data representing the next value in the sequence of values within the object
  2. done : 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); // 185

 

转载于:https://www.cnblogs.com/Answer1215/p/7873331.html

你可能感兴趣的文章
yahoo web性能优化最佳实践
查看>>
开始一个简单的ASP.NET Web API 2 (C#)
查看>>
jqGrid对可空日期类型数据转换时的错误处理
查看>>
分布式技术一周技术动态 2015.12.06
查看>>
HAOI2011 problem a
查看>>
第3章 汇编语言基础
查看>>
使用Qt框架开发http服务器问题的记录
查看>>
Nintex Forms Drop-Down "z-index"
查看>>
Windows Server 2012 IIS 8 - 安装SSL证书
查看>>
javaagent bytebuddy动态加载原理解析
查看>>
数据结构与算法-绪论
查看>>
RxSwift学习--高阶函数 / 操作符(上)
查看>>
React 新特性 Hooks 讲解及实例(三)
查看>>
关于Python装饰器,这11条你不知道,别说你精通Python装饰器
查看>>
阿里云配置Https
查看>>
Pr学习笔记
查看>>
Tex学习笔记
查看>>
二维数组中的查找
查看>>
java面向对象基础总结
查看>>
java第一次实验总结&第三周总结
查看>>