1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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_循环进行迭代">使用 <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>
|