mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-11 09:14:23 +08:00
117 lines
5.5 KiB
HTML
117 lines
5.5 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p>forEach 方法根据集合中元素的顺序,对每个元素都执行提供的 callback 函数一次。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript"><code><em>mySet</em>.forEach(<em>callback</em>[, <em>thisArg</em>])</code></code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt><code>callback</code></dt>
|
||
<dd>每个元素都会执行的函数</dd>
|
||
<dt><code>thisArg</code></dt>
|
||
<dd>当执行callback函数时候,可以当作this来使用。</dd>
|
||
</dl>
|
||
<h2 id="描述">描述</h2>
|
||
<p>这个forEach方法会针对集合中的每个元素执行提供的callback函数一次。 对于那些已经被删除的元素,它是不会执行的,但是,对于元素是undefined的情况则相反。</p>
|
||
<p><code>callback</code> 有三个参数:</p>
|
||
<ul>
|
||
<li>元素的值</li>
|
||
<li>元素的索引</li>
|
||
<li>将要遍历的集合对象</li>
|
||
</ul>
|
||
<p>Set对象中没有索引值(keys),前2个参数都是包含在<a class="new" href="/zh-CN/docs/Web/API/Set" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>Set</code></a>中的元素的值(<strong>values</strong>),所以该回调函数和<a href="Reference/Global_Objects/Map/foreach" title="forEach() 方法将会以插入顺序对 Map 对象中的每一个键值对执行一次参数中提供的回调函数。"><code>Map</code></a> 以及<a href="Reference/Global_Objects/Array/forEach" title="forEach() 方法对数组的每个元素执行一次提供的函数。"><code>Array</code></a>的forEach函数是一致的。</p>
|
||
<p>如果提供了一个thisArg参数给forEach函数,当被调用时,该参数将会传递到callback回调函数中来指代this值。否则,this值会是undefined。由回调所能观察到的this对象是根据<a href="https://developer.mozilla.orgReference/Operators/this">通常的规则来决定的,这是由一个函数决定的</a>。</p>
|
||
<p>forEach函数在第一次调用回调函数前确定所要处理的元素的范围。在调用forEach之后添加到Set对象的元素将不会被回调函数访问。如果Set对象的现有元素改变或者删除了,那么该元素传给回调函数的值会是forEach函数访问它们时的值。被删除的元素没有被访问。</p>
|
||
<p><code>forEach</code>函数对每个Set对象的元素执行一次回调;它不会返回任何值。</p>
|
||
<h2 id="例子">例子</h2>
|
||
<h3 id="输出Set对象的内容">输出Set对象的内容</h3>
|
||
<p><span class="outputBox-2liU7_0">以下代码为Set对象中的每个元素记录一行:</span></p>
|
||
<pre><code class="language-js">function logSetElements(value1, value2, set) {
|
||
console.log("s[" + value1 + "] = " + value2);
|
||
}
|
||
|
||
new Set(["foo", "bar", undefined]).forEach(logSetElements);
|
||
|
||
// logs:
|
||
// "s[foo] = foo"
|
||
// "s[bar] = bar"
|
||
// "s[undefined] = undefined"
|
||
</code></pre>
|
||
<h2 id="规范">规范</h2>
|
||
<table class="standard-table">
|
||
<tbody>
|
||
<tr>
|
||
<th scope="col">Specification</th>
|
||
<th scope="col">Status</th>
|
||
<th scope="col">Comment</th>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-set.prototype.foreach" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Set.prototype.forEach</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Initial definition.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility">Browser compatibility</h2>
|
||
<p></p><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div><p></p>
|
||
<div id="compat-desktop">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Chrome</th>
|
||
<th>Firefox (Gecko)</th>
|
||
<th>Internet Explorer</th>
|
||
<th>Opera</th>
|
||
<th>Safari</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td>38</td>
|
||
<td><a href="/en-US/Firefox/Releases/25" title="Released on 2013-10-29.">25.0</a> (25.0)</td>
|
||
<td>11</td>
|
||
<td>25</td>
|
||
<td>7.1</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<div id="compat-mobile">
|
||
<table class="compat-table">
|
||
<tbody>
|
||
<tr>
|
||
<th>Feature</th>
|
||
<th>Android</th>
|
||
<th>Chrome for Android</th>
|
||
<th>Firefox Mobile (Gecko)</th>
|
||
<th>IE Mobile</th>
|
||
<th>Opera Mobile</th>
|
||
<th>Safari Mobile</th>
|
||
</tr>
|
||
<tr>
|
||
<td>Basic support</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>38</td>
|
||
<td>25.0 (25.0)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td>8</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h2 id="See_also">See also</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Array/forEach" title="forEach() 方法对数组的每个元素执行一次提供的函数。"><code>Array.prototype.forEach()</code></a></li>
|
||
<li><a href="Reference/Global_Objects/Map/forEach" title="forEach() 方法将会以插入顺序对 Map 对象中的每一个键值对执行一次参数中提供的回调函数。"><code>Map.prototype.forEach()</code></a></li>
|
||
</ul>
|
||
</article> |