What is a javascript iterator

青灯夜游
Release: 2021-12-07 17:30:55
Original
1607 people have browsed it

In JavaScript, an iterator is a special object that has some proprietary interfaces specially designed for the iterative process. All iterator objects have a next() method, which returns a Result object. The iterator stores an internal pointer to the location of the current value in the collection.

What is a javascript iterator

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. Definition of iterator:

Iterator is a special object, which has some special features for iteration processDesigned proprietary interface, all iterator objects have a next() method, and each call returns a result object. The result object has two attributes: one is value, indicating the next value to be returned; the other is done, which is a Boolean value that returns true when there is no more data to return. The iterator also saves an internal pointer to point to the position of the value in the current collection. Each time the next() method is called, the next available value will be returned.

If next is called after the last value is returned, () method, then the value of the attribute done in the returned object is true, and the attribute value contains the value finally returned by the iterator. This return value is not part of the data set. It is similar to the return value of the function and is the last value in the function call process. A method of passing information to the caller at one time. If there is no relevant data, undefined

will be returned. 2. Why does the iterator appear?

Background premise:

I believe you must have used a for loop, and also had a null pointer Quoting the question from : For example, the length of the array is only 5, but your index reaches 6. A slight mistake in the logic will cause the program to not run normally.

The problem that the iterator wants to solve is:

Solve or reduce Using a similar for loop process, An error occurred while accessing the variable collection. For example: null pointer reference.

For example:

When we use a for loop to traverse a collection, it is easy to make mistakes, but if we use something like forEach, it will reduce the number of empty spaces. Pointer reference problem.

array.forEach(element => {
	// 此时可以不用i来操作集合了。            
});
Copy after login

3. Use js to implement a simple iterator

function myIterator(list) {
    let i = 0;
    return {
        next: function() {
            let done = (i >= list.length);
            let value = !done ? list[i++] : undefined;
            return {
                done: done,
                value: value
            };
        }
    };
}
Copy after login

It can be seen from the function definition:

  • The return value of the function is an object . In the object, the key is next and the value is the function;

  • Every time next() is called, i 1, and returns an object at the same time, the object is the element of the collection;

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is a javascript iterator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!