This is the 21st day of my participation in the August Text Challenge.More challenges in August

The iterator

An iterator is a special type of object with proprietary interfaces designed for the iterative process. All iterator objects have a next() method that returns a result object each time it is called. The result object has two attributes: value, which represents the next value to be returned; The other is done, which is a Boolean that returns true if there is no more data to return. The iterator also holds an internal pointer to the location of the value in the current collection, and each time the next() method is called, the next available value is returned. If the next() method is called after the last value is returned, the value of the property done in the returned object is true, and the value contains the final value returned by the iterator. This return value is not part of the data set. It is similar to the return value of a function, and is the last method to pass information to the caller during the function call. Return undefined if there is no relevant data.

The generator

A generator is a function that returns an iterator, denoted by an asterisk (*) after the function keyword. The new keyword yield is used in the function. The asterisk can be next to the function keyword, or you can add a space in the middle, as in:

function *createIterator() {  
yield 1;  yield 2;  yield 3;
}let iterator = createIterator();  
// The generator is called just like a normal function, except that it returns an iterator console.log(iterator.next().value);
// console.log(iterator.next().value);  
// console.log(iterator.next().value);  // 3
Copy the code

Use the yield keyword to return any value or expression, so you can add elements to iterators in batches via generator functions. For example, you can use the yield keyword in a loop

function *createIterator(items) { for (let i = 0; i < items.length; i++) { yield items[i]; }} let iterator = createIterator([1,2,3]); // The generator is called just like a normal function, except that it returns an iterator console.log(iterator.next()); // {value: 1, done: false}console.log(iterator.next()); // {value: 2, done: false}console.log(iterator.next()); // {value: 3, done: false}console.log(iterator.next()); // {value: undefined, done: true}// All subsequent calls return the same content console.log(iterator.next()); // {value: undefined, done: true}Copy the code

The yield keyword can only be used inside a generator. Using it elsewhere will cause the program to run a syntax error, even if it is used inside a function of the generator.

Generator functional expression

Let createIterator = function *(items) {// code block

}

This example is the same as the previous one

You cannot use arrow functions to create generators

Iterable and for-of loops

Iterables have the Symbol. Iterator property and are objects closely related to iterators. Symbol.iterator returns an iterator to a dependent object using the specified function. In ECMAScript6, all collection objects (arrays, sets, and maps) and strings are iterable objects that have default iterators in them. The new ECMAScript feature for-of loops requires these capabilities of the iterable.

Because generators assign values to the symbol. iterator property by default, all iterators created by generators are iterables

The for-of loop calls the iterable’s next () method every time it executes and stores the iterator’s value property in a variable. The loop keeps doing this until the done property of the returned object is true. Take this example:

let values = [1.2.3];
// Normal for-OR loop
for (let num of values) {  
    console.log(num); }// Print the result:
   / / 1
   / / 2
   / / 3
    // Access the default iterator, as follows
    let iterator = values[Symbol.iterator]();
    console.log(iterator.next());  // {value: 1, done: false}
    console.log(iterator.next());  // {value: 2, done: false}
    console.log(iterator.next());  // {value: 3, done: false}
    console.log(iterator.next());  // {value: undefined, done: true}
Copy the code

Checks whether an object is iterable

function isIterable(object) {  
return typeof object[Symbol.iterator] === 'function'; }console.log(isIterable([1.2.3])); // true
console.log(isIterable('hello')); // true
console.log(isIterable(new Map())); // true
console.log(isIterable(new Set())); // true
console.log(isIterable(new WeakMap())); // false
console.log(isIterable(new WeakSet())); // false
Copy the code

Built-in iterators

Collection object iterator

There are three types of collection objects in ECMAScript6: arrays, Maps, and sets. To better access the contents of the object, all three types of objects have the following iterators built in:

  • Entries () returns an iterator of key-value pairs.
  • Values () returns an iterator whose value is the value of the collection.
  • Keys () returns an iterator with the value of all the key names in the collection.
  • Entries () an iterator

Each time the next() method is called, the entries iterator returns an array of two elements representing the key and value of each element in the collection. If the object being iterated over is an array, the first element is a numeric index; In the case of a Set, the first element and the second element are values (values in a Set are used as both keys and values). In the case of a Map collection, the first element is the key name. Here is an example of the use of the entries() iterator:

Entries () an iterator

let colors = ['red'.'green'.'blur'];
let tracking = new Set([132.465.956]);
let data = new Map(a); data.set('title'.'ECMAScript 6');
data.set('format'.'ebook');
for(let entry of colors.entries()) {  
console.log(entry); }// Print the result
// [0, 'red']
// [1, 'green'] 
// [2, 'blur'] 
for(let entry of tracking.entries()) { 
console.log(entry); }// Print the result
/ / [132, 132]
/ / [465, 465]
/ / [956, 956]
for(let entry of data.entries()) {  
console.log(entry); }// Print the result
// ['title', 'ECMAScript 6']// ['format', 'ebook']
Copy the code

Values () iterators

Calling the values() iterator returns all the values in the collection, for example:

let colors = ['red'.'green'.'blur'];
let tracking = new Set([132.465.956]);
let data = new Map(a); data.set('title'.'ECMAScript 6');
data.set('format'.'ebook');
for(let entry of colors.values()) {
console.log(entry); }// Print the result
// 'red'
// 'green'
// 'blur'
for(let entry of tracking.values()) { 
console.log(entry); }// Print the result
/ / 132
/ / 465
/ / 956
for(let entry of data.values()) {  
console.log(entry); }// Print the result
// 'ECMAScript 6'
// 'ebook'
Copy the code

The keys () an iterator

The keys() iterator returns each key that exists in the collection. If the array is traversed, the numeric key is returned. No other properties of the array itself are returned. In the case of a Set, keys() and values() return the same iterator because the keys and values are the same; In the case of a Map() collection, the keys() iterator returns each individual key. Take a look at this example:

let colors = ['red'.'green'.'blur'];
let tracking = new Set([132.465.956]);
let data = new Map(a); data.set('title'.'ECMAScript 6');
data.set('format'.'ebook');
for(let entry of colors.keys()) { 
console.log(entry); }// Print the result
/ / 0
/ / 1
/ / 2
for(let entry of tracking.keys()) {  
console.log(entry); }// Print the result
/ / 132
/ / 465
/ / 956
for(let entry of data.keys()) {  
console.log(entry); }// Print the result
// 'title'
// 'format'
Copy the code

I see a long road ahead, I will search up and down, refueling together, learn the front end

Welcome to comment ~