aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/@@iterator
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/array/@@iterator
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/@@iterator')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/array/@@iterator/index.html114
1 files changed, 114 insertions, 0 deletions
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
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>@@iterator</strong></code> 属性和 {{jsxref("Array.prototype.values()", "Array.prototype.values()")}} 属性的初始值是同一个函数对象。</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js"><var>arr</var>[Symbol.iterator]()</pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p>数组的 <strong>iterator </strong>方法,默认情况下,与 {{jsxref("Array.prototype.values()", "values()")}} 返回值相同, <code>arr[Symbol.iterator]</code> 则会返回 {{jsxref("Array.prototype.values()", "values()")}} 函数。</p>
+
+<h2 id="示例">示例</h2>
+
+<h3 id="使用_for...of_循环进行迭代"><font face="Open Sans, Arial, sans-serif">使用 </font><code>for...of</code> 循环进行迭代</h3>
+
+<pre class="brush: js">var arr = ['a', 'b', 'c', 'd', 'e'];
+var eArr = arr[Symbol.iterator]();
+// 浏览器必须支持 for...of 循环
+for (let letter of eArr) {
+ console.log(letter);
+}
+</pre>
+
+<h3 id="另一种迭代方式">另一种迭代方式</h3>
+
+<pre class="brush: js">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
+</pre>
+
+<h3 id="Use_Case_for_brace_notation">Use Case for brace notation</h3>
+
+<p>The use case for this syntax over using the dot notation (<code>Array.prototype.values()</code>) 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 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/@@iterator">String</a> object or a custom object.</p>
+
+<pre class="brush: js">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</pre>
+
+<h2 id="规范">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范名称</th>
+ <th scope="col">规范状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-array.prototype-@@iterator', 'Array.prototype[@@iterator]()')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>首次定义</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype-@@iterator', 'Array.prototype[@@iterator]()')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+
+
+<p>{{Compat("javascript.builtins.Array.@@iterator")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.keys()")}}</li>
+ <li>{{jsxref("Array.prototype.entries()")}}</li>
+ <li>{{jsxref("Array.prototype.forEach()")}}</li>
+ <li>{{jsxref("Array.prototype.every()")}}</li>
+ <li>{{jsxref("Array.prototype.some()")}}</li>
+ <li>{{jsxref("Array.prototype.values()")}}</li>
+</ul>