Difference between Iterator and Iterable [duplicat

2020-07-10 10:05发布

What is the difference between Iterator and Iterable? Is one sub-type of the other or not? What are the actual differences in some real applications?

I am trying to read some tutorials and specifications, but they are all very complicated.

(I am using ES6 and Babel, if that helps.)

1条回答
ゆ 、 Hurt°
2楼-- · 2020-07-10 10:47

From Exploring ES6 by Dr. Axel Rauschmayer:

An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators.

An iterator is a pointer for traversing the elements of a data structure (think cursors in databases).

查看更多
登录 后发表回答