From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../reference/global_objects/iterator/index.html | 138 +++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 files/ru/web/javascript/reference/global_objects/iterator/index.html (limited to 'files/ru/web/javascript/reference/global_objects/iterator') diff --git a/files/ru/web/javascript/reference/global_objects/iterator/index.html b/files/ru/web/javascript/reference/global_objects/iterator/index.html new file mode 100644 index 0000000000..0a55a44af9 --- /dev/null +++ b/files/ru/web/javascript/reference/global_objects/iterator/index.html @@ -0,0 +1,138 @@ +--- +title: Iterator +slug: Web/JavaScript/Reference/Global_Objects/Iterator +tags: + - JavaScript + - Reference +translation_of: Archive/Web/Iterator +--- +
{{jsSidebar("Objects")}}
+ +
 
+ +
+

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 objectIterator 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.
+
+ +

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

+ +

Not supported. Used to be in Firefox in versions prior to Firefox 57.

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf