Iterator
Non-standard. The
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.
Syntax
JavaScript
Copy Code
Iterator(<var>object</var>, [keyOnly])
Parameters
object
- Object to iterate over properties.
keyOnly
- If
keyOnly
is truthy value,Iterator.prototype.next
returnsproperty_name
only.
Description
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.
Properties
Iterator.prototype[@@iterator]
- Returns a function that returns iterator object, that conforms to iterator protocol.
Methods
Iterator.prototype.next
- Returns next item in the
[property_name, property_value]
format orproperty_name
only. It throwsStopIteration
if there are no more items.
Examples
Iterating over properties of an object
JavaScript
Copy Code
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
Iterating over properties of an object with legacy destructuring for-in
statement
JavaScript
Copy Code
var a = { x: 10, y: 20, }; for (var [name, value] in Iterator(a)) { console.log(name, value); // x 10 // y 20 }
Iterating with for-of
JavaScript
Copy Code
var a = { x: 10, y: 20, }; for (var [name, value] of Iterator(a)) { // @@iterator is used console.log(name, value); // x 10 // y 20 }
Iterates over property name
JavaScript
Copy Code
var a = { x: 10, y: 20, }; for (var name in Iterator(a, true)) { console.log(name); // x // y }
Passing Generator instance
JavaScript
Copy Code
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 }
Passing Iterator instance
JavaScript
Copy Code
var a = { x: 10, y: 20, }; var i = Iterator(a); console.log(i == Iterator(i)); // true
Specifications
Non-standard. Not part of any current standards document.
Browser compatibility
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 |
See also
License
© 2016 Mozilla 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