首页 技术 正文
技术 2022年11月15日
0 收藏 910 点赞 2,798 浏览 2071 个字

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); //
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920