Iterator和Iterable之间的区别

Iterator和Iterable之间的区别

问题描述:

Iterator和Iterable有什么区别?是另一种亚型吗?在某些实际应用中实际有什么区别?

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.

(如果有帮助,我正在使用ES6和Babel.)

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

来自探索ES6 Axel Rauschmayer博士撰写:

From Exploring ES6 by Dr. Axel Rauschmayer:

可迭代的是一种数据结构,希望使其元素可被公众访问.它通过实现一个键为Symbol.iterator的方法来实现.该方法是迭代器的工厂.

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).