[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); // 185
分类:
ES6
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2015-11-21 [Falcor] Intro to JSON Graph
2014-11-21 [ES6] 09. Destructuring Assignment -- 2
2014-11-21 [ES6] 08. Destructuring Assignment -- 1
2013-11-21 8. 利用反射机制, ListArray,intent来实现多Activity的切换
2013-11-21 7. Add song to Phone
2013-11-21 6. Activity life cycle