5 ms·
"You can also create your own iterable objects. Normally this is done via the yield keyword (discussed below in Generators) but it could be done explicitly by r
by Luyt 16y ago
"You can also create your own iterable objects. Normally this is done via the yield keyword (discussed below in Generators) but it could be done explicitly by returning an object that has __iterator__"
This clearly seems to be borrowed from Python; even the double underscores are present, which looks a little odd in javascript.
- tlrobinson 16y agoThere is a precedent for double underscores in JavaScript, for example the __proto__ property. However that convention is usually reserved for private APIs, which arguably __iterator__ isn't.
- msy 16y agoThe double underscore syntax is used in Javascript already for getters and setters: this.__defineGetter__
- munificent 16y agoThe naming here comes from Mozilla's earlier implementation (https://developer.mozilla.org/en/JavaScript/Guide/Iterators_and_Generators https://developer.mozilla.org/en/JavaScript/Guide/Iterators_...). Personally, I would have preferred iterate(). Traceur doesn't use the same Python-style semantics as Mozilla does: it doesn't throw an error to stop iteration. Instead, it's closer to Java or C# where moveNext() returns false if the iterator is done.