Iterator function is a SpiderMonkey-specific feature, and will be removed at some point. For future-facing usages, consider using for..of loops and the iterator protocol.The Iterator function returns an object which implements legacy iterator protocol and iterates over enumerable properties of an object.
Iterator(object, [keyOnly])
objectkeyOnlykeyOnly is truthy value, Iterator.prototype.next returns property_name only.Returns Iterator instance that iterates over object. Iterator instance returns [property_name, property_value] array for each iteration if keyOnly is falsy, otherwise, if keyOnly is truthy, it returns property_name for each iteration. If object is the Iterator instance or Generator instance, it returns object itself.
Iterator.prototype[@@iterator]Iterator.prototype.next[property_name, property_value] format or property_name only. It throws StopIteration if there are no more items.var a = {
x: 10,
y: 20,
};
var iter = Iterator(a);
console.log(iter.next()); // ["x", 10]
console.log(iter.next()); // ["y", 20]
console.log(iter.next()); // throws StopIteration
for-in statementvar a = {
x: 10,
y: 20,
};
for (var [name, value] in Iterator(a)) {
console.log(name, value); // x 10
// y 20
}
for-of
var a = {
x: 10,
y: 20,
};
for (var [name, value] of Iterator(a)) { // @@iterator is used
console.log(name, value); // x 10
// y 20
}
var a = {
x: 10,
y: 20,
};
for (var name in Iterator(a, true)) {
console.log(name); // x
// y
}
function* f() {
yield 'a';
yield 'b';
}
var g = f();
console.log(g == Iterator(g)); // true
for (var v in Iterator(g)) {
console.log(v); // a
// b
}
var a = {
x: 10,
y: 20,
};
var i = Iterator(a);
console.log(i == Iterator(i)); // true
Non-standard. Not part of any current standards document.
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | No support | (Yes) | No support | No support | No support |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | No support | No support | (Yes) | No support | No support | No support |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator