blob: 65ea0ae37f0cdd2d57a00d391b26a0ade1db86fe (
plain)
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
|
---
title: 'String.prototype[@@iterator]()'
slug: Web/JavaScript/Reference/Global_Objects/String/@@iterator
translation_of: Web/JavaScript/Reference/Global_Objects/String/@@iterator
---
<div>{{JSRef}}</div>
<p><strong><code>[@@iterator]()</code></strong> 方法返回一个新的Iterator对象,它遍历字符串的代码点,返回每一个代码点的字符串值。</p>
<p>{{EmbedInteractiveExample("pages/js/string-iterator.html")}}</p>
<p>The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><code><var>string</var>[Symbol.iterator]</code></pre>
<h3 id="返回值">返回值</h3>
<p>一个新的Iterator对象。</p>
<h2 id="示例">示例</h2>
<h3 id="使用iterator"><code>使用[@@iterator]()</code></h3>
<pre class="brush:js">var string = 'A\uD835\uDC68';
var strIter = string[Symbol.iterator]();
console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // "\uD835\uDC68"
</pre>
<h3 id="通过_for..of_使用iterator"><code>通过</code> <code>for..of 使用[@@iterator]()</code></h3>
<pre class="brush:js">var string = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A';
for (var v of string) {
console.log(v);
}
// "A"
// "\uD835\uDC68"
// "B"
// "\uD835\uDC69"
// "C"
// "\uD835\uDC6A"
</pre>
<h2 id="规范">规范</h2>
<table>
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-string.prototype-@@iterator', 'String.prototype[@@iterator]()')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-string.prototype-@@iterator', 'String.prototype[@@iterator]()')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<table class="standard-table">
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
{{Compat}}
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a></li>
</ul>
|