mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-11 00:54:06 +08:00
119 lines
5.8 KiB
HTML
119 lines
5.8 KiB
HTML
<article id="wikiArticle">
|
||
<div> </div>
|
||
<div></div>
|
||
<p><code><strong>forEach()</strong></code> 方法将会以插入顺序对 Map 对象中的每一个键值对执行一次参数中提供的回调函数。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript"><code><em>myMap</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>可选,<code>callback</code> 执行时其 <code>this</code> 的值。</dd>
|
||
<dt>
|
||
<h3 id="返回值">返回值</h3>
|
||
<p><a href="Reference/Global_Objects/undefined" title="undefined是全局对象的一个属性。也就是说,它是全局作用域的一个变量。undefined的最初值就是原始数据类型undefined。"><code>undefined</code></a>.</p>
|
||
</dt>
|
||
</dl>
|
||
<h2 id="概述">概述</h2>
|
||
<p><code>forEach</code> 方法将对 <code>Map</code> 中真实存在的每一个元素执行一次参数中提供的回调函数,它不会对任何已经被删除的元素执行调用。然而,它还会对键存在而值为 <code>undefined</code> 的元素执行调用。</p>
|
||
<p><font face="Consolas, Liberation Mono, Courier, monospace"><code>callback</code> 函数有<strong>三个参数</strong></font>:</p>
|
||
<ul>
|
||
<li><strong><code>value</code></strong> - 元素的值</li>
|
||
<li><strong><code>key</code></strong> - 元素的键</li>
|
||
<li><strong><code>Map</code></strong> - 当前正在被遍历的<code>对象</code></li>
|
||
</ul>
|
||
<p>如果参数 <code>forEach</code> 带有一个 <code>thisArg</code> 参数,在调用的时候,这个参数将传给 <code>callback</code> 函数作为其 this 的值。否则,函数将默认使用 <code>undefined</code> 传给 <code>callback</code> 函数作为其 <code>this</code> 值。最终,这里的 <code>this </code>的值将依照 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">函数观测并确定 <code>this</code> 的相关规则</a> 由 <code>callback</code> 函数最终观察到的值决定。</p>
|
||
<p><code>forEach</code> 函数处理的元素的范围为第一次执行 callback 函数时 Map 对象中的键值对集合。在 <code>Map</code> 对象调用 <code>forEach</code> 之后加入的元素将不会被调用 <code>callback</code> 函数。如果在调用 <code>forEach</code> 之后 <code>Map</code> 对象中的被改变或者删除了,它们传给 <code>callback</code> 函数的值将会变成 <code>forEach</code> 函数访问它们时的值;<code>callback</code> 不会访问其调用其间被删除的元素。</p>
|
||
<p><code>forEach </code>仅仅是对 <code>Map</code> 对象中的每一个元素执行一遍 <code>callback</code> 函数,然后直接返回 <code>undefined</code>。</p>
|
||
<h2 id="示例">示例</h2>
|
||
<h3 id="打印一个_Map_对象中的元素">打印一个 <code>Map </code>对象中的元素</h3>
|
||
<p>下面的代码在一行中打印一个 <code>Map</code> 对象的每一个元素:</p>
|
||
<pre><code class="language-js">function logMapElements(value, key, map) {
|
||
console.log("m[" + key + "] = " + value);
|
||
}
|
||
Map([["foo", 3], ["bar", {}], ["baz", undefined]]).forEach(logMapElements);
|
||
// logs:
|
||
// "m[foo] = 3"
|
||
// "m[bar] = [object Object]"
|
||
// "m[baz] = 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-map.prototype.foreach" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Map.prototype.forEach</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Initial definition.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</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="相关链接">相关链接</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/Set/forEach" title="forEach 方法根据集合中元素的顺序,对每个元素都执行提供的 callback 函数一次。"><code>Set.prototype.forEach()</code></a></li>
|
||
</ul>
|
||
</article> |