Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Relawan kami belum menerjemahkan artikel ini ke dalam Bahasa Indonesia . Bergabunglah dan bantu kami menyelesaikan pekerjaan ini!

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

Iterator(object, [keyOnly])

Parameters

object
Object to iterate over properties.
keyOnly
If keyOnly is truthy value, Iterator.prototype.next returns property_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 or property_name only. It throws StopIteration if there are no more items.

Examples

Iterating over properties of an object

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

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

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

var a = {
  x: 10,
  y: 20,
};

for (var name in Iterator(a, true)) {
  console.log(name);   // x
                       // y
}

Passing Generator instance

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

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

Tag Dokumen dan Kontributor

 Terakhir diperbarui oleh: arai,