From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/array/@@iterator/index.html | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/array/@@iterator/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/@@iterator') diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/@@iterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/@@iterator/index.html new file mode 100644 index 0000000000..31fe03d8b1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/@@iterator/index.html @@ -0,0 +1,114 @@ +--- +title: 'Array.prototype[@@iterator]()' +slug: Web/JavaScript/Reference/Global_Objects/Array/@@iterator +tags: + - Array + - ECMAScript 2015 + - Iterator + - JavaScript + - Method + - Prototype + - 原型 + - 参考 + - 循环 + - 方法 + - 迭代 +translation_of: Web/JavaScript/Reference/Global_Objects/Array/@@iterator +--- +
{{JSRef}}
+ +

@@iterator 属性和 {{jsxref("Array.prototype.values()", "Array.prototype.values()")}} 属性的初始值是同一个函数对象。

+ +

语法

+ +
arr[Symbol.iterator]()
+ +

返回值

+ +

数组的 iterator 方法,默认情况下,与 {{jsxref("Array.prototype.values()", "values()")}} 返回值相同, arr[Symbol.iterator] 则会返回 {{jsxref("Array.prototype.values()", "values()")}} 函数。

+ +

示例

+ +

使用 for...of 循环进行迭代

+ +
var arr = ['a', 'b', 'c', 'd', 'e'];
+var eArr = arr[Symbol.iterator]();
+// 浏览器必须支持 for...of 循环
+for (let letter of eArr) {
+  console.log(letter);
+}
+
+ +

另一种迭代方式

+ +
var arr = ['a', 'b', 'c', 'd', 'e'];
+var eArr = arr[Symbol.iterator]();
+console.log(eArr.next().value); // a
+console.log(eArr.next().value); // b
+console.log(eArr.next().value); // c
+console.log(eArr.next().value); // d
+console.log(eArr.next().value); // e
+
+ +

Use Case for brace notation

+ +

The use case for this syntax over using the dot notation (Array.prototype.values()) is in a case where you don't know what object is going to be ahead of time. If you have a function that takes an iterator and then iterate over the value, but don't know if that Object is going to have a [Iterable].prototype.values method. This could be a built-in object like String object or a custom object.

+ +
function logIterable(it) {
+  var iterator = it[Symbol.iterator]();
+  // 浏览器必须支持 for...of 循环
+  for (let letter of iterator) {
+      console.log(letter);
+  }
+}
+
+// Array
+logIterable(['a', 'b', 'c']);
+// a
+// b
+// c
+
+// string
+logIterable('abc');
+// a
+// b
+// c
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
规范名称规范状态备注
{{SpecName('ES6', '#sec-array.prototype-@@iterator', 'Array.prototype[@@iterator]()')}}{{Spec2('ES6')}}首次定义
{{SpecName('ESDraft', '#sec-array.prototype-@@iterator', 'Array.prototype[@@iterator]()')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Array.@@iterator")}}

+ +

参见

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