diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-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/symbol/asynciterator')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html new file mode 100644 index 0000000000..f0c05da9ab --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/symbol/asynciterator/index.html @@ -0,0 +1,76 @@ +--- +title: Symbol.asyncIterator +slug: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator +translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator +--- +<div>{{JSRef}}</div> + +<p><code><strong>Symbol.asyncIterator</strong></code> 符号指定了一个对象的默认异步迭代器。如果一个对象设置了这个属性,它就是异步可迭代对象,可用于<code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of">for await...of</a></code>循环。</p> + + + +<h2 id="描述">描述</h2> + +<p><code>Symbol.asyncIterator</code> 是一个用于访问对象的<code>@@asyncIterator</code>方法的内建符号。一个异步可迭代对象必须要有<code>Symbol.asyncIterator</code>属性。</p> + +<p>{{js_property_attributes(0,0,0)}}</p> + +<h2 id="示例">示例</h2> + +<h3 id="自定义异步可迭代对象">自定义异步可迭代对象</h3> + +<p>你可以通过设置<code>[Symbol.asyncIterator]</code>属性来自定义异步可迭代对象。</p> + +<pre class="brush: js">const myAsyncIterable = new Object(); +myAsyncIterable[Symbol.asyncIterator] = async function*() { + yield "hello"; + yield "async"; + yield "iteration!"; +}; + +(async () => { + for await (const x of myAsyncIterable) { + console.log(x); + // expected output: + // "hello" + // "async" + // "iteration!" + } +})(); +</pre> + +<h3 id="内建异步可迭代对象">内建异步可迭代对象</h3> + +<p>目前没有默认设定了<code>[Symbol.asyncIterator]</code>属性的JavaScript内建的对象。不过,WHATWG(网页超文本应用技术工作小组)Streams会被设定为第一批异步可迭代对象,<code>[Symbol.asyncIterator]</code> 最近已在设计规范中落地。</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">标准</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ES2018', '#sec-symbol.asynciterator', 'Symbol.asyncIterator')}}</td> + <td>{{Spec2('ES2018')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{compat("javascript.builtins.Symbol.asyncIterator")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of">for await... of</a></li> +</ul> |