Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement javascript iterator

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to implement the javascript iterator". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to implement the javascript iterator.

In javascript, an iterator is a special object with proprietary interfaces designed specifically for the iterative process, and all iterator objects have a next () method that returns a result object for each call. The iterator holds an internal pointer to the location of the values in the current collection.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

I. the definition of iterator:

An iterator is a special object with proprietary interfaces designed specifically for the iterative process. All iterator objects have a next () method that returns a result object for each call. The result object has two properties: value, which represents the next value to be returned, and done, which is a Boolean value that returns true when there is no more data to return. The iterator also holds an internal pointer to the location of the values 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 property value contains the final value returned by the iterator. This return value is not part of the dataset. Similar to the return value of the function, it is the last method to pass information to the caller during the function call. If there is no relevant data, it returns undefined.

Second, why do iterators appear?

Background premise:

I'm sure you've used for loops and have had problems with null pointer references: for example, the length of the array is only 5, but your index is up to 6. The slightest error in logic results in the program not working properly.

The problem that the iterator wants to solve is:

Resolve or reduce errors in accessing a collection of variables in the use of similar for loops. For example: null pointer reference.

Take a chestnut:

It is easy to make mistakes when we use for loops to iterate through collections, but if we use something like forEach, we will reduce the problem of null pointer references.

Array.forEach (element = > {/ / now you can manipulate the collection without I. Implement a simple iterator function myIterator (list) {let I = 0; return {next: function () {let done = (I > = list.length); let value =! done? List: undefined; return {done: done, value: value};};}

As can be seen from the function definition:

The return value of the function is an object, in which key is next and value is function.

Each time next () is called, iTun1 returns an object, which is the element of the collection.

At this point, I believe you have a deeper understanding of "how to implement the javascript iterator". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report