JavaScript Iterators

I went through the MDN (Mozilla Developer Network) and came across iterators and generators

So naturally, I tried the code snippets shown on the page in 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? Is the Iterator function obsolete or something else? Am I missing a point? Thanks for the help and time :-)

+8
javascript
source share
6 answers

From this thread :

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

V8 is intended for connecting compatible with AOs, implementing ECMAScript in WebKit / Safari. Thus, he implements a number of non-standard ECMAScript extensions, which are also located in AO, and most of them are also in Mozilla JavaScript languages.

It is not planned to add non-standard functions that are not included in AO-V8.

Note: JSC means JavaScript Core - implementation of WebKit ECMAScript.

+3
source share

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

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

Standard output:

 1 2 3 

In the worst case, you can easily design an iterator object, have not passed the full test, but if there are any errors, you can quickly get this work.

 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()); } 
+6
source share

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

+4
source share
 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 
+2
source share

This means that Chrome v21 does not support this JavaScript feature. This is part of specification 1.7. Trying this may help to explicitly support 1.7 support in Chrome.

+1
source share

For chrome, you can use this

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

here that you might find interesting

0
source share

All Articles