8

I was going through MDN (Mozilla Developer Network) and came across Iterators and generators

So naturally, I tried the snippets of code given in the page on Google Chrome v21. To be specific, this code:

var it = Iterator(lang);
for (var pair in it)
  print(pair); // prints each [key, value] pair in turn

However, the console returns this error message:

ReferenceError: Iterator is not defined

Why's that? Is the Iterator function deprecated or something? Am I missing a point? Thank you for your help and time :-)

Hirvesh
  • 7,636
  • 15
  • 59
  • 72
  • 3
    Somewhat related: [Google Chrome - javascript version](http://stackoverflow.com/questions/300185/google-chrome-javascript-version) – j.w.r Aug 22 '12 at 18:46

6 Answers6

6

Arrays have a built in map function that acts like an iterator.

[1,2,3].map(function(input){console.log(input)});

Standard Out:

1
2
3

Worst case you could easily design an iterator object, didn't full test this but if there are any bugs you should be able to quickly get this working.

var Iterator = function(arr){ return {
    index : -1,
    hasNext : function(){ return this.index <= arr.length; },
    hasPrevious: function(){ return this.index > 0; },

    current: function(){ return arr[ this["index"] ]; },

    next : function(){
        if(this.hasNext()){
            this.index = this.index + 1;            
            return this.current();
        } 
        return false;
    },

    previous : function(){
        if(this.hasPrevious()){
            this.index = this.index - 1
            return this.current();
        }
        return false;
    }
}   
};

var iter = Iterator([1,2,3]);

while(iter.hasNext()){
    console.log(iter.next());
}
Dan
  • 1,179
  • 2
  • 10
  • 18
4

window.Iterator AFAIK only exists in Firefox, not WebKit.

David Hellsing
  • 106,495
  • 44
  • 176
  • 212
3

From this thread:

V8 is an implementation of ECMAScript, not JavaScript. The latter is a non-standardized extension of ECMAScript made by Mozilla.

V8 is intended to be plug-in compatible with JSC, the ECMAScript implementation in WebKit/Safari. As such it implements a number of non-standard extensions of ECMAScript that are also in JSC, and most of these are also in Mozilla's JavaScript languages.

There is no plan to add non-standard features that are not in JSC to V8.

Note: JSC stands for JavaScript Core - the WebKit ECMAScript implementation.

Community
  • 1
  • 1
RichardTowers
  • 4,682
  • 1
  • 26
  • 43
2
var makeIterator = function (collection, property) {
    var agg = (function (collection) {
    var index = 0;
    var length = collection.length;

    return {
      next: function () {
        var element;
          if (!this.hasNext()) {
            return null;
          }
          element = collection[index][property];
          index = index + 1;
          return element;
        },
      hasNext: function () {
        return index < length;
      },
      rewind: function () {
        index = 0;
      },
      current: function () {
        return collection[index];
      }
    };
 })(collection);

 return agg;
};


var iterator = makeIterator([5,8,4,2]);

console.log(iterator.current())//5
console.log(  iterator.next() )
console.log(iterator.current()) //8
console.log(iterator.rewind());
console.log(iterator.current()) //5
Michael Phelps
  • 3,451
  • 7
  • 36
  • 64
1

It means that Chrome v21 does not support that feature of JavaScript. It's part of the 1.7 spec. Trying this might help for specifying explicitly 1.7 support in Chrome.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

For chrome you could use this

var someArray = [1, 5, 7];
var someArrayEntries = someArray.entries();

here is link, which you could find interesting

andrey
  • 347
  • 3
  • 11