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 C
For chrome you could use this
var someArray = [1, 5, 7];
var someArrayEntries = someArray.entries();
here is link, which you could find interesting
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.
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.
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());
}
window.Iterator
AFAIK only exists in Firefox, not WebKit.