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 :-)

asked Aug 22, 2012 at 18:42
1

7 Answers 7

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());
}
answered Jan 4, 2013 at 21:54
Sign up to request clarification or add additional context in comments.

2 Comments

Maybee hasNext : function(){ return this.index < arr.length-1; }, ?
for object add var arr= Object.keys(arr);
4

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

answered Aug 22, 2012 at 18:45

Comments

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.

answered Aug 22, 2012 at 18:46

1 Comment

This is outdated information.
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
answered Jul 7, 2014 at 7:47

Comments

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.

answered Aug 22, 2012 at 18:47

Comments

0

For chrome you could use this

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

here is link, which you could find interesting

answered Mar 21, 2015 at 8:50

Comments

0

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);

answered Jan 25, 2025 at 20:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.