[ES6] The Iterator Protocol
ES6 The protocol Iterator
2023-09-14 09:00:51 时间
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); // 185
相关文章
- es6数组和对象常用方法
- es6数组方法图解
- 每天3分钟,重学ES6-ES12(十八) CJS
- ES6新特性之Map和Set
- 传统的回调函数与 ES6中的promise回调以及 ES7 的async/await终极的异步同步化
- 一文搞懂ES6的Map,Map和Object如何选择
- 前端基础进阶(十七):详解 ES6 Modules
- 每天3分钟,重学ES6-ES12(七)ES10 新增内容
- ES6的Map用法详解
- babel ES6 转换 ES5 实现原理
- ES6-异步遍历器(三)
- 【踩坑实录】-java.sql.SQLException: The MySQL server is running with the LOCK_WRITE_GROWTH option so it ca
- ES6新特性: 解构赋值
- ORA-32019: The parameter SPFILE cannot be updated in the server parameter file. ORACLE 报错 故障修复 远程处理
- ORA-48409: The ADR homes exceeds the maximum number [string] ORACLE 报错 故障修复 远程处理
- ORA-48491: The program name is too long, exceeds the maximum length [string] ORACLE 报错 故障修复 远程处理
- ORA-48929: The trace record size exceeded the max size that can be read [string] ORACLE 报错 故障修复 远程处理
- ORA-53253: The metadata for the new DICOM object is invalid. ORACLE 报错 故障修复 远程处理
- ORA-55569: The UNDO_RETENTION parameter value should be atmost string, the _highthreshold_undoretention setting. ORACLE 报错 故障修复 远程处理
- ORA-13619: The procedure argument string is greater than the maximum allowable length of string characters. ORACLE 报错 故障修复 远程处理
- ORA-13904: The file has been dropped and recreated during the procedure call. ORACLE 报错 故障修复 远程处理
- MySQL 5.7.17:The Latest Upgrade of the Database System(mysql5.7.17)
- 行界面并掌握常用命令Note: The original prompt mentions 25character title but the provided Chinese alternative mentions 25word title. This response assumes the latter is the intended instruction.(怎样进入linux命令)
- Discover the Power of Linux with YLMF: The UserFriendly Operating System(linuxylmf)