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 :-)
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 :-)
Share Improve this question asked Aug 22, 2012 at 18:42 HirveshHirvesh 7,98216 gold badges62 silver badges74 bronze badges 1- 3 Somewhat related: Google Chrome - javascript version – j.w.r Commented Aug 22, 2012 at 18:46
7 Answers
Reset to default 6Arrays 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());
}
window.Iterator
AFAIK only exists in Firefox, not WebKit.
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.
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
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.
For chrome you could use this
var someArray = [1, 5, 7];
var someArrayEntries = someArray.entries();
here is link, which you could find interesting
Iterator
was obsolete, but got a new life:
The question refers to a long obsolete implementation of an Iterator
function in Firefox back in the days of JavaScript 1.7, which was not part of an ECMA-262 standard at the time.
However, as of ECMAScript 2025, there is now again a global Iterator
. It is an abstract constructor/class such that all core JavaScript functions that return an iterator, inherit from Iterator.prototype
, and thereby get access to a set of iterator helper methods.
So now you can do:
const res = Array(1000).keys() // Iterator for 0, 1, 2, 3, 4, ...
.map(n => n*n) // Iterator for 0, 1, 4, 16, 24, ...
.filter(n => n % 17 == 1) // Iterator for 1, 256, ...
.toArray(); // Consume iterator into array
console.log(res);