I am reading the MDN manual on iterators and generators , and I have completed the following example:
function Range(low, high){ this.low = low; this.high = high; } Range.prototype.__iterator__ = function(){ return new RangeIterator(this); }; function RangeIterator(range){ this.range = range; this.current = this.range.low; } RangeIterator.prototype.next = function(){ if (this.current > this.range.high) throw StopIteration; else return this.current++; }; var range = new Range(3, 5); for (var i in range) { console.log(i); }
But I get this:
low high __iterator__
Where he was supposed to return:
3 4 5
Is it possible to implement this as expected in Node.js?
Note: I am using node --harmony myscript.js .
** Change **
It should also be noted that I know that __iterator__ is only a Mozilla function . I would like to know what is equivalent in Node.js. Or what's the best.
Also, I would not want to use a generator for this, since I want the iteration to not contain any conditional tests.
My conclusion is that the only equivalent is
Range.prototype.each = function (cb) { for (var i = this.low; i <= this.high; i++) { cb(i); } };
Are there any alternatives?
source share