admin管理员组

文章数量:1384383

I am reading the MDN guide on Iterators and generators and I implemented 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 it should have returned :

3
4
5

Is it possible to implement this as expected in Node.js?

Note: I am using node --harmony myscript.js.

** Edit **

I should also note that I am aware that __iterator__ is a Mozilla-feature only. I'd like to know what's the equivalent in Node.js. Or what's the next best thing.

Also, I'd like to not use a generator for this as I want the iteration to be free of 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);
  }
};

//...
range.each(function (i) {
  console.log(i);
});

Are there alternatives?

I am reading the MDN guide on Iterators and generators and I implemented 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 it should have returned :

3
4
5

Is it possible to implement this as expected in Node.js?

Note: I am using node --harmony myscript.js.

** Edit **

I should also note that I am aware that __iterator__ is a Mozilla-feature only. I'd like to know what's the equivalent in Node.js. Or what's the next best thing.

Also, I'd like to not use a generator for this as I want the iteration to be free of 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);
  }
};

//...
range.each(function (i) {
  console.log(i);
});

Are there alternatives?

Share Improve this question edited Apr 14, 2014 at 6:53 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Feb 27, 2014 at 15:26 Yanick RochonYanick Rochon 53.6k29 gold badges145 silver badges225 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I think your code had a bug where you wrote for (var i in range) instead of for (var i of range). The in keyword prints out the object's properties, you want to use of to actually get the iterator values.

Anyway, Node.js now has better harmony support since the question was asked. Here's an updated code sample which works out of the box on Node 4.2. The __iterator__ name was changed to Symbol.iterator, and the StopIteration exception isn't used.

"use strict";

function Range(low, high){
  this.low = low;
  this.high = high;
}
Range.prototype[Symbol.iterator] = function(){
  return new RangeIterator(this);
};

function RangeIterator(range){
  this.range = range;
  this.current = this.range.low;
}
RangeIterator.prototype.next = function(){
  let result = {done: this.current > this.range.high, value: this.current};
  this.current++;
  return result;
};

var range = new Range(3, 5);
for (var i of range) {
  console.log(i);
}

Yes it is.

Generators and iterators are part of ECMAScript 6 / JavaScript 1.7. Node has support for them but you need to activate them, when starting you script.

For example:

node --harmony_generators --harmony_iteration your_script.js

Take a look at this blog post for more information.

本文标签: javascriptNodejs Iterator interfaceStack Overflow