[ES6] The Iterator Protocol
ES6 The protocol Iterator
2023-09-14 08:59:17 时间
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》笔记——块级作用域绑定(1)
- 原生ES6写的Web游戏:ES6-Mario,小美女,小帅哥快来玩啊~~
- ES6之前模拟Map数据结构的写法
- es6数组求和
- [ES6] Generators
- [ES6] When should use Map instead of Object
- [ES6] 03. The let keyword -- 1
- [ES6] 02. Traceur compiler and Grunt
- sharepoint2010:The number of items in this list exceeds the list view threshold, which is 20000 items.
- Caused by: org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must end with the ';' delimiter.
- [Javascript] Understand common misconceptions about ES6's const keyword
- [ES6] Object.assign (with defaults value object)
- ES6学习之路7----set数据结构
- Unexpected XML declaration. The XML declaration must be the first node in the document and no white
- JavaScript ES6 Fetch API时需要注意的一个Cookie问题
- Error Domain=NSCocoaErrorDomain Code=518 “The file couldn’t be saved because the specified URL type
- The resource could not be loaded because the App Transport Security policy requires the use of a sec
- 解决The type or namespace name 'XXXX' does not exist in the namespace 'XXXXXXXXX' 的错误
- 成功解决The following specifications were found to be incompatible with the existing python installation
- 已解决The above exception was the direct cause of the following exception:
- js 动态添加class封装(es6语法)
- JS与ES6高级编程学习笔记(一)——JavaScript核心组成
- `Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the
- 编译webrtc报错:ERROR: The installation of the Chrome OS default fonts failed.
- 前端js进阶之ES6 Promise(承诺)用法小结笔记、详细解释(resolve,catch,catch)历史最通俗易懂的承诺