diff options
author | Peter Bengtsson <mail@peterbe.com> | 2021-07-23 11:05:59 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-23 11:05:59 -0400 |
commit | 8079aacdb9fd11d5eebcf16a7d7e0f5a31466cf2 (patch) | |
tree | 959e30f9fc8d0855690494eb85ba8196ea347668 /files/zh-cn/web | |
parent | a8b2d80e4c706f5fc6209b267f2bcc5bc89cb6d3 (diff) | |
download | translated-content-8079aacdb9fd11d5eebcf16a7d7e0f5a31466cf2.tar.gz translated-content-8079aacdb9fd11d5eebcf16a7d7e0f5a31466cf2.tar.bz2 translated-content-8079aacdb9fd11d5eebcf16a7d7e0f5a31466cf2.zip |
wasn't supposed to be orphaned (zh-cn) (#1674)
* wasn't supposed to be orphaned (zh-cn)
* fix redirects
* fix slug front-matter
Diffstat (limited to 'files/zh-cn/web')
15 files changed, 1625 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html new file mode 100644 index 0000000000..f0528e4189 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/@@iterator/index.html @@ -0,0 +1,104 @@ +--- +title: Map.prototype[@@iterator]() +slug: Web/JavaScript/Reference/Global_Objects/Map/@@iterator +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@iterator +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@iterator +browser-compat: javascript.builtins.Map.@@iterator +--- +<div>{{JSRef}}</div> + +<p><code><strong>@@iterator</strong></code><strong> </strong>属性的初始值与 {{jsxref("Map.prototype.entries()", "entries")}} 属性的初始值是同一个函数对象。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-@@iterator.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>[Symbol.iterator]</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>map 的 <strong>iterator</strong> 函数默认就是 {{jsxref("Map.prototype.entries()", "entries()")}} 函数。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_iterator()">使用 <code>[@@iterator]()</code></h3> + +<pre class="brush:js">var myMap = new Map(); +myMap.set('0', 'foo'); +myMap.set(1, 'bar'); +myMap.set({}, 'baz'); + +var mapIter = myMap[Symbol.iterator](); +//返回的其实是个generator +console.log(mapIter.next().value); // ["0", "foo"] +console.log(mapIter.next().value); // [1, "bar"] +console.log(mapIter.next().value); // [Object, "baz"] +</pre> + +<h3 id="在for..of中使用iterator()">在<code>for..of中</code>使用<code>[@@iterator]()</code> </h3> + +<pre class="brush:js">var myMap = new Map(); +myMap.set('0', 'foo'); +myMap.set(1, 'bar'); +myMap.set({}, 'baz'); + +for (const entry of myMap) { + console.log(entry); +} +// ["0", "foo"] +// [1, "bar"] +// [{}, "baz"] + +for (var v of myMap) { + console.log(v); +} + +// 0: foo +// 1: bar +// [Object]: baz</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>{{SpecName('ES2015', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype-@@iterator', 'Map.prototype[@@iterator]()')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("javascript.builtins.Map.@@iterator")}}</p> + +<h2 id="另见">另见</h2> + +<ul> + <li>{{jsxref("Map.prototype.entries()")}}</li> + <li>{{jsxref("Map.prototype.keys()")}}</li> + <li>{{jsxref("Map.prototype.values()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html new file mode 100644 index 0000000000..b34c711929 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/@@species/index.html @@ -0,0 +1,74 @@ +--- +title: get Map[@@species] +slug: Web/JavaScript/Reference/Global_Objects/Map/@@species +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@species +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@species +browser-compat: javascript.builtins.Map.@@species +--- +<div>{{JSRef}}</div> + +<p> <code><strong>Map[@@species]</strong></code> 访问器属性会返回一个 <code>Map</code> 构造函数.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Map[Symbol.species] +</pre> + +<h2 id="描述">描述</h2> + +<p>The species accessor property returns the default constructor for <code>Map</code> objects. Subclass constructors may over-ride it to change the constructor assignment.</p> + +<h2 id="案例">案例</h2> + +<p>The species property returns the default constructor function, which is the <code>Map</code> constructor for <code>Map</code> objects:</p> + +<pre class="brush: js">Map[Symbol.species]; // function Map()</pre> + +<p>In a derived collection object (e.g. your custom map <code>MyMap</code>), the <code>MyMap</code> species is the <code>MyMap</code> constructor. However, you might want to overwrite this, in order to return parent <code>Map</code> objects in your derived class methods:</p> + +<pre class="brush: js">class MyMap extends Map { + // 重写覆盖 MyMap species to the parent Map constructor + static get [Symbol.species]() { return Map; } +}</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>{{SpecName('ES2015', '#sec-get-map-@@species', 'get Map [ @@species ]')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-map-@@species', 'get Map [ @@species ]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.Map.@@species")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Symbol.species")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html new file mode 100644 index 0000000000..46132abe7f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/@@tostringtag/index.html @@ -0,0 +1,104 @@ +--- +title: Map.prototype[@@toStringTag] +slug: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +original_slug: Web/JavaScript/Reference/Global_Objects/Map/@@toStringTag +browser-compat: javascript.builtins.Map.@@toStringTag +--- +<div>{{JSRef}}</div> + +<p> <strong><code>Map[@@toStringTag]</code></strong> 的初始值是"Map".</p> + +<div>{{js_property_attributes(0,0,1)}}</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">Map[Symbol.toStringTag]</pre> + +<h2 id="示例">示例</h2> + +<pre class="brush:js">Object.prototype.toString.call(new Map()) // "[object Map]" +</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>{{SpecName('ES2015', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype-@@tostringtag', 'Map.prototype[@@toStringTag]')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<p>{{CompatibilityTable}} </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>{{ CompatChrome(44.0) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatNo }}</td> + <td>{{ CompatChrome(44.0) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(44.0)}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html new file mode 100644 index 0000000000..a83f6d78bb --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/clear/index.html @@ -0,0 +1,81 @@ +--- +title: Map.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/Map/clear +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/clear +original_slug: Web/JavaScript/Reference/Global_Objects/Map/clear +browser-compat: javascript.builtins.Map.clear +--- +<div>{{JSRef}}</div> + +<p><code>clear()</code>方法会移除Map对象中的所有元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-clear.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>.clear();</code> +</pre> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref("undefined")}}.</p> + +<h2 id="示例">示例</h2> + +<h3 id="调用clear方法">调用<code>clear</code>方法</h3> + +<pre class="brush: js">var myMap = new Map(); +myMap.set("bar", "baz"); +myMap.set(1, "foo"); + +myMap.size; // 2 +myMap.has("bar"); // true + +myMap.clear(); + +myMap.size; // 0 +myMap.has("bar") // false +</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>{{SpecName('ES2015', '#sec-map.prototype.clear', 'Map.prototype.clear')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.clear', 'Map.prototype.clear')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("javascript.builtins.Map.clear")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Map")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html new file mode 100644 index 0000000000..6bbd943f7f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/delete/index.html @@ -0,0 +1,82 @@ +--- +title: Map.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/Map/delete +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/delete +original_slug: Web/JavaScript/Reference/Global_Objects/Map/delete +browser-compat: javascript.builtins.Map.delete +--- +<div>{{JSRef}}</div> + +<p><strong> </strong><code><strong><strong>delete()</strong></strong></code><strong> </strong>方法用于移除 <code>Map</code> 对象中指定的元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-delete.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="brush: js">myMap.delete(key);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>必须。从 <code>Map</code> 对象中移除的元素的键。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>Boolean</dt> + <dd>如果 <code>Map</code> 对象中存在该元素,则移除它并返回<em> <code>true</code></em>;否则如果该元素不存在则返回 <code><em>false</em></code>。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="使用_delete_方法">使用 <code>delete</code> 方法</h3> + +<pre class="brush: js">var myMap = new Map(); +myMap.set("bar", "foo"); + +myMap.delete("bar"); // 返回 true。成功地移除元素 +myMap.has("bar"); // 返回 false。"bar" 元素将不再存在于 Map 实例中 +</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>{{SpecName('ES6', '#sec-map.prototype.delete', 'Map.prototype.delete')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.delete', 'Map.prototype.delete')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Map.delete")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Map")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html new file mode 100644 index 0000000000..db24574461 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/entries/index.html @@ -0,0 +1,79 @@ +--- +title: Map.prototype.entries() +slug: Web/JavaScript/Reference/Global_Objects/Map/entries +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/entries +original_slug: Web/JavaScript/Reference/Global_Objects/Map/entries +browser-compat: javascript.builtins.Map.entries +--- +<div>{{JSRef}}</div> + +<p><code><strong>entries()</strong></code> 方法返回一个新的包含 <code>[key, value]</code> 对的 <code><strong>Iterator</strong></code> 对象,返回的迭代器的迭代顺序与 <code>Map</code> 对象的插入顺序相同。</p> + +<p>{{EmbedInteractiveExample("pages/js/map-prototype-entries.html")}}</p> + +<h2 id="语法">语法</h2> + +<pre><code><var>myMap</var>.entries()</code> +</pre> + +<h3 id="返回值">返回值</h3> + +<p>一个新的 {{jsxref("Map")}} 迭代器对象.</p> + +<h2 id="示例">示例</h2> + +<h3 id="entries()的使用"><code>entries()</code>的使用</h3> + +<pre class="brush:js">var myMap = new Map(); +myMap.set("0", "foo"); +myMap.set(1, "bar"); +myMap.set({}, "baz"); + +var mapIter = myMap.entries(); + +console.log(mapIter.next().value); // ["0", "foo"] +console.log(mapIter.next().value); // [1, "bar"] +console.log(mapIter.next().value); // [Object, "baz"] +</pre> + +<h2 id="规范">规范</h2> + +<table> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.entries', 'Map.prototype.entries')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-map.prototype.entries', 'Map.prototype.entries')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Map.entries")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Map.prototype.keys()")}}</li> + <li>{{jsxref("Map.prototype.values()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html new file mode 100644 index 0000000000..301214807e --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/foreach/index.html @@ -0,0 +1,111 @@ +--- +title: Map.prototype.forEach() +slug: Web/JavaScript/Reference/Global_Objects/Map/forEach +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/forEach +original_slug: Web/JavaScript/Reference/Global_Objects/Map/forEach +browser-compat: javascript.builtins.Map.forEach +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary"><code><strong>forEach()</strong></code> 方法按照插入顺序依次对 <code>Map</code> 中每个键/值对执行一次给定的函数</span></p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-foreach.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code><var>myMap</var>.forEach(<var>callback([<var>value</var>][,<var>key</var>][,<var>map</var>])[, <var>thisArg</var>])</var></code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><var>callback</var></code></dt> + <dd> + <p><code><var>myMap</var></code> 中每个元素所要执行的函数。它具有如下的参数</p> + + <dl> + <dt><code><var>value</var></code> {{Optional_Inline}}</dt> + <dd>每个迭代的值。</dd> + <dt><code><var>key</var></code> {{Optional_Inline}}</dt> + <dd>每个迭代的键。</dd> + <dt><code><var>map</var></code> {{Optional_Inline}}</dt> + <dd>被迭代的map(上文语法框中的 <code><var>myMap</var></code>)。</dd> + </dl> + </dd> + <dt><code><var>thisArg</var></code> {{Optional_Inline}}</dt> + <dd>在 <code><var>callback</var></code> 执行中使用的 <code>this</code> 的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>{{jsxref("undefined")}}.</p> + +<h2 id="描述">描述</h2> + +<p><code>forEach</code> 方法会对map中每个真实存在的键执行一次给定的 <code><var>callback</var></code> 函数。它不会对被删除的键执行函数。然而,它会对每个值为 <code>undefined</code> 的键执行函数。</p> + +<p><code><var>callback</var></code> 接收<strong>三个参数</strong>:</p> + +<ul> + <li>当前的 <code><var>value</var></code></li> + <li>当前的 <code><var>key</var></code></li> + <li>正在被遍历的 <strong><code>Map</code> 对象</strong></li> +</ul> + +<p>如果 <code>forEach</code> 中含有 <code><var>thisArg</var></code> 参数,那么每次 <code><var>callback</var></code> 被调用时,都会被用作 <code>this</code> 的值。否则,<code>undefined</code> 将会被用作 <code>this</code> 的值。按照<a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">函数观察到 <code>this</code> 的常用规则</a>,<code><var>callback</var></code> 函数最终可观察到 <code>this</code> 值。</p> + +<p>每个值只被访问一次,除非它被删除了或者在 <code>forEach</code> 结束前被改变了。<code><var>callback</var></code> 不会对在被访问前就删除的元素执行。在 <code>forEach</code> 结束前被添加的元素将会被访问。</p> + +<p><code>forEach</code> 会对 <code>Map</code> 对象中的每个元素执行一次 <code><var>callback</var></code>。它不会返回值。</p> + +<h2 id="示例">示例</h2> + +<h3 id="输出一个_Map_对象中的内容">输出一个 <code>Map</code> 对象中的内容</h3> + +<p>以下的代码在每行中打印一个 <code>Map</code> 对象中的元素</p> + +<pre class="brush:js notranslate">function logMapElements(value, key, map) { + console.log(`map.get('${key}') = ${value}`) +} +new Map([['foo', 3], ['bar', {}], ['baz', undefined]]).forEach(logMapElements) +// logs: +// "map.get('foo') = 3" +// "map.get('bar') = [object Object]" +// "map.get('baz') = undefined" +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.foreach', 'Map.prototype.forEach')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("javascript.builtins.Map.forEach")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Array.prototype.forEach()")}}</li> + <li>{{jsxref("Set.prototype.forEach()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html new file mode 100644 index 0000000000..934f5d9d08 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/get/index.html @@ -0,0 +1,83 @@ +--- +title: Map.prototype.get() +slug: Web/JavaScript/Reference/Global_Objects/Map/get +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/get +original_slug: Web/JavaScript/Reference/Global_Objects/Map/get +browser-compat: javascript.builtins.Map.get +--- +<div>{{JSRef}}</div> + +<p><code><strong>get()</strong></code> 方法返回某个 <code>Map</code> 对象中的一个指定元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-get.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>.get(key);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>必须参数,也是它唯一的参数,要从目标 <code>Map</code> 对象中获取的元素的键。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回一个 <code>Map</code> 对象中与指定键相关联的值,如果找不到这个键则返回 <code>undefined</code>。</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_get_方法">使用 <code>get</code> 方法</h3> + +<pre class="brush: js">var myMap = new Map(); +myMap.set("bar", "foo"); + +myMap.get("bar"); // 返回 "foo" +myMap.get("baz"); // 返回 undefined +</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('ES2015', '#sec-map.prototype.get', 'Map.prototype.get')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.get', 'Map.prototype.get')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Map.get")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Map.prototype.set()")}}</li> + <li>{{jsxref("Map.prototype.has()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html new file mode 100644 index 0000000000..db61579557 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/has/index.html @@ -0,0 +1,123 @@ +--- +title: Map.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/Map/has +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/has +original_slug: Web/JavaScript/Reference/Global_Objects/Map/has +browser-compat: javascript.builtins.Map.has +--- +<div>{{JSRef}}</div> + +<p>方法<code><strong>has()</strong></code> 返回一个bool值,用来表明map 中是否存在指定元素.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>.has(key);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>必填. 用来检测是否存在指定元素的键值.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<dl> + <dt>Boolean</dt> + <dd>如果指定元素存在于Map中,则返回true。其他情况返回false</dd> +</dl> + +<h2 id="案例">案例</h2> + +<h3 id="使用has方法">使用has方法</h3> + +<pre class="brush: js">var myMap = new Map(); +myMap.set("bar", "foo"); + +myMap.has("bar"); // returns true +myMap.has("baz"); // returns false +</pre> + +<h2 id="规范列表">规范列表</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">规范名称</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-map.prototype.has', 'Map.prototype.has')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>浏览器</th> + <th>Chrome谷歌</th> + <th>Firefox (Gecko)火狐</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>兼容版本</td> + <td>38</td> + <td>{{CompatGeckoDesktop("13.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>浏览器</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>兼容版本</td> + <td>{{CompatNo}}</td> + <td>38</td> + <td>{{CompatGeckoMobile("13.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Map.prototype.set()")}}</li> + <li>{{jsxref("Map.prototype.get()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/index.html new file mode 100644 index 0000000000..88ab546cf7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/index.html @@ -0,0 +1,341 @@ +--- +title: Map +slug: Web/JavaScript/Reference/Global_Objects/Map +tags: + - Class + - ECMAScript 2015 + - JavaScript + - Map + - Reference + - Polyfill +translation_of: Web/JavaScript/Reference/Global_Objects/Map +original_slug: Web/JavaScript/Reference/Global_Objects/Map +browser-compat: javascript.builtins.Map +--- +<div> +<div>{{JSRef}}</div> +</div> + +<p><strong><code>Map</code></strong> 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者{{Glossary("Primitive", "原始值")}}) 都可以作为一个键或一个值。</p> + +<dl> +</dl> + +<h2 id="描述">描述</h2> + +<p>一个Map对象在迭代时会根据对象中元素的插入顺序来进行 — 一个 {{jsxref("Statements/for...of", "for...of")}} 循环在每次迭代后会返回一个形式为[key,value]的数组。</p> + +<h3 id="键的相等Key_equality">键的相等(Key equality)</h3> + +<ul> + <li>键的比较是基于 <code><a href="/zh-CN/docs/Web/JavaScript/Equality_comparisons_and_sameness#零值相等">sameValueZero</a></code> 算法:</li> + <li>{{jsxref("NaN")}} 是与 <code>NaN</code> 相等的(虽然 <code>NaN !== NaN</code>),剩下所有其它的值是根据 <code>===</code> 运算符的结果判断是否相等。</li> + <li>在目前的ECMAScript规范中,<code>-0</code>和<code>+0</code>被认为是相等的,尽管这在早期的草案中并不是这样。有关详细信息,请参阅<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Map#浏览器兼容性">浏览器兼容性</a> 表中的“Value equality for -0 and 0”。</li> +</ul> + +<h3 id="Objects_和_maps_的比较">Objects 和 maps 的比较</h3> + +<p>{{jsxref("Object", "Objects")}} 和 <code>Maps</code> 类似的是,它们都允许你按键存取一个值、删除键、检测一个键是否绑定了值。因此(并且也没有其他内建的替代方式了)过去我们一直都把对象当成 <code>Maps</code> 使用。不过 <code>Maps</code> 和 <code>Objects</code> 有一些重要的区别,在下列情况里使用 <code>Map</code> 会是更好的选择:</p> + +<table class="standard-table"> + <thead> + <tr> + <th scope="row"></th> + <th scope="col">Map</th> + <th scope="col">Object</th> + </tr> + </thead> + <tbody> + <tr> + <th scope="row">意外的键</th> + <td><code>Map</code> 默认情况不包含任何键。只包含显式插入的键。</td> + <td> + <p>一个 <code>Object</code> 有一个原型, 原型链上的键名有可能和你自己在对象上的设置的键名产生冲突。</p> + + <div class="blockIndicator note"> + <p><strong>注意:</strong> 虽然 ES5 开始可以用 <code>Object.create(null)</code> 来创建一个没有原型的对象,但是这种用法不太常见。</p> + </div> + </td> + </tr> + <tr> + <th scope="row">键的类型</th> + <td>一个 <code>Map</code>的键可以是<strong>任意值</strong>,包括函数、对象或任意基本类型。</td> + <td>一个<code>Object</code> 的键必须是一个 {{jsxref("String")}} 或是{{jsxref("Symbol")}}。</td> + </tr> + <tr> + <th scope="row">键的顺序</th> + <td> + <p><code>Map</code> 中的 key 是有序的。因此,当迭代的时候,一个 <code>Map</code> 对象以插入的顺序返回键值。</p> + </td> + <td> + <p>一个 <code>Object</code> 的键是无序的</p> + + <div class="blockIndicator note"> + <p>注意:自ECMAScript 2015规范以来,对象<em>确实</em>保留了字符串和Symbol键的创建顺序; 因此,在只有字符串键的对象上进行迭代将按插入顺序产生键。</p> + </div> + </td> + </tr> + <tr> + <th scope="row">Size</th> + <td> <code>Map</code> 的键值对个数可以轻易地通过{{jsxref("Map.prototype.size", "size")}} 属性获取</td> + <td><code>Object</code> 的键值对个数只能手动计算</td> + </tr> + <tr> + <th scope="row">迭代</th> + <td><code>Map</code> 是 <a href="/en-US/docs/Web/JavaScript/Guide/iterable">iterable</a> 的,所以可以直接被迭代。</td> + <td>迭代一个<code>Object</code>需要以某种方式获取它的键然后才能迭代。</td> + </tr> + <tr> + <th scope="row">性能</th> + <td> + <p>在频繁增删键值对的场景下表现更好。</p> + </td> + <td> + <p>在频繁添加和删除键值对的场景下未作出优化。</p> + </td> + </tr> + </tbody> +</table> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{jsxref("Global_Objects/Map/Map", "Map()")}}</dt> + <dd>创建 <code>Map</code> 对象</dd> +</dl> + +<h2 id="属性">属性</h2> + +<dl> + <dt><code>Map.length</code></dt> + <dd>属性 length 的值为 0 。<br> + 想要计算一个<code>Map</code> 中的条目数量, 使用 {{jsxref("Map.prototype.size")}}.</dd> + <dt>{{jsxref("Map.@@species", "get Map[@@species]")}}</dt> + <dd>本构造函数用于创建派生对象。</dd> + <dt>{{jsxref("Map.prototype")}}</dt> + <dd>表示 <code>Map</code> 构造器的原型。 允许添加属性从而应用于所有的 <code>Map</code> 对象。</dd> +</dl> + +<h2 id="Map_实例"><code>Map</code> 实例</h2> + +<p><font face="Open Sans, Arial, sans-serif">所有的 </font><code>Map</code> 对象实例都会继承 {{jsxref("Map.prototype")}}。</p> + +<h3 id="属性_2">属性</h3> + +<p>{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','属性')}}</p> + +<h3 id="方法">方法</h3> + +<p>{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/Map/prototype','方法')}}</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_Map_对象">使用 <code>Map</code> 对象</h3> + +<pre class="brush: js">let myMap = new Map(); + +let keyObj = {}; +let keyFunc = function() {}; +let keyString = 'a string'; + +// 添加键 +myMap.set(keyString, "和键'a string'关联的值"); +myMap.set(keyObj, "和键keyObj关联的值"); +myMap.set(keyFunc, "和键keyFunc关联的值"); + +myMap.size; // 3 + +// 读取值 +myMap.get(keyString); // "和键'a string'关联的值" +myMap.get(keyObj); // "和键keyObj关联的值" +myMap.get(keyFunc); // "和键keyFunc关联的值" + +myMap.get('a string'); // "和键'a string'关联的值" + // 因为keyString === 'a string' +myMap.get({}); // undefined, 因为keyObj !== {} +myMap.get(function() {}); // undefined, 因为keyFunc !== function () {}</pre> + +<h3 id="将_NaN_作为_Map_的键">将 <code>NaN</code> 作为 <code>Map</code> 的键</h3> + +<p><code>NaN</code> 也可以作为<code>Map</code>对象的键。虽然 <code>NaN</code> 和任何值甚至和自己都不相等(<code>NaN !== NaN</code> 返回true),但下面的例子表明,<code>NaN</code>作为Map的键来说是没有<span><span>区别的</span></span>:</p> + +<pre class="brush: js">let myMap = new Map(); +myMap.set(NaN, "not a number"); + +myMap.get(NaN); // "not a number" + +let otherNaN = Number("foo"); +myMap.get(otherNaN); // "not a number" +</pre> + +<h3 id="使用_for..of_方法迭代_Map">使用 <code>for..of</code> 方法迭代 <code>Map</code></h3> + +<p><code>Map</code>可以使用<code>for..of</code>循环来实现迭代:</p> + +<pre class="brush: js">let myMap = new Map(); +myMap.set(0, "zero"); +myMap.set(1, "one"); +for (let [key, value] of myMap) { + console.log(key + " = " + value); +} +// 将会显示两个log。一个是"0 = zero"另一个是"1 = one" + +for (let key of myMap.keys()) { + console.log(key); +} +// 将会显示两个log。 一个是 "0" 另一个是 "1" + +for (let value of myMap.values()) { + console.log(value); +} +// 将会显示两个log。 一个是 "zero" 另一个是 "one" + +for (let [key, value] of myMap.entries()) { + console.log(key + " = " + value); +} +// 将会显示两个log。 一个是 "0 = zero" 另一个是 "1 = one"</pre> + +<h3 id="使用_forEach_方法迭代_Map">使用 <code>forEach()</code> 方法迭代 <code>Map</code></h3> + +<p><code>Map</code>也可以通过<code>forEach()</code>方法迭代:</p> + +<pre class="brush: js">myMap.forEach(function(value, key) { + console.log(key + " = " + value); +}) +// 将会显示两个logs。 一个是 "0 = zero" 另一个是 "1 = one" +</pre> + +<h3 id="Map_与数组的关系"><code>Map</code> 与数组的关系</h3> + +<pre class="brush: js">let kvArray = [["key1", "value1"], ["key2", "value2"]]; + +// 使用常规的Map构造函数可以将一个二维键值对数组转换成一个Map对象 +let myMap = new Map(kvArray); + +myMap.get("key1"); // 返回值为 "value1" + +// 使用Array.from函数可以将一个Map对象转换成一个二维键值对数组 +console.log(Array.from(myMap)); // 输出和kvArray相同的数组 + +// 更简洁的方法来做如上同样的事情,使用展开运算符 +console.log([...myMap]); + +// 或者在键或者值的迭代器上使用Array.from,进而得到只含有键或者值的数组 +console.log(Array.from(myMap.keys())); // 输出 ["key1", "key2"] +</pre> + +<h3 id="复制或合并_Maps">复制或合并 <code>Maps</code></h3> + +<p>Map 能像数组一样被复制:</p> + +<pre class="brush: js">let original = new Map([ + [1, 'one'] +]); + +let clone = new Map(original); + +console.log(clone.get(1)); // one +console.log(original === clone); // false. 浅比较 不为同一个对象的引用 +</pre> + +<div class="blockIndicator note"> +<p>重要:请记住,<em>数据本身</em>未被克隆。</p> +</div> + +<p>Map对象间可以进行合并,但是会保持键的唯一性。</p> + +<pre class="brush: js">let first = new Map([ + [1, 'one'], + [2, 'two'], + [3, 'three'], +]); + +let second = new Map([ + [1, 'uno'], + [2, 'dos'] +]); + +// 合并两个Map对象时,如果有重复的键值,则后面的会覆盖前面的。 +// 展开运算符本质上是将Map对象转换成数组。 +let merged = new Map([...first, ...second]); + +console.log(merged.get(1)); // uno +console.log(merged.get(2)); // dos +console.log(merged.get(3)); // three</pre> + +<p>Map对象也能与数组合并:</p> + +<pre class="brush: js">let first = new Map([ + [1, 'one'], + [2, 'two'], + [3, 'three'], +]); + +let second = new Map([ + [1, 'uno'], + [2, 'dos'] +]); + +// Map对象同数组进行合并时,如果有重复的键值,则后面的会覆盖前面的。 +let merged = new Map([...first, ...second, [1, 'eins']]); + +console.log(merged.get(1)); // eins +console.log(merged.get(2)); // dos +console.log(merged.get(3)); // three</pre> + +<h2 id="使用说明">使用说明</h2> + +<p>请注意!为Map设置对象属性也是可以的,但是可能引起大量的混乱。</p> + +<p>所以,你还是<em>可以这样做.</em>..</p> + +<pre class="syntaxbox example-bad brush js">let wrongMap = new Map() +wrongMap['bla'] = 'blaa' +wrongMap['bla2'] = 'blaaa2' + +console.log(wrongMap) // Map { bla: 'blaa', bla2: 'blaaa2' } +</pre> + +<p>...但是,这样做的话,它的行为会不符合预期:</p> + +<pre class="syntaxbox example-bad brush js">wrongMap.has('bla') // false +wrongMap.delete('bla') // false +console.log(wrongMap) // Map { bla: 'blaa', bla2: 'blaaa2' }</pre> + +<p>无论如何,和正确用法比较起来,几乎没有什么不同: </p> + +<pre class="syntaxbox brush js example-good">let myMap = new Map() +myMap.set('bla','blaa') +myMap.set('bla2','blaa2') +console.log(myMap) // Map { 'bla' => 'blaa', 'bla2' => 'blaa2' } + +myMap.has('bla') // true +myMap.delete('bla') // true +console.log(myMap) // Map { 'bla2' => 'blaa2' }</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-map-objects', 'Map')}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("javascript.builtins.Map")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html new file mode 100644 index 0000000000..59c1b50654 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/keys/index.html @@ -0,0 +1,80 @@ +--- +title: Map.prototype.keys() +slug: Web/JavaScript/Reference/Global_Objects/Map/keys +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/keys +original_slug: Web/JavaScript/Reference/Global_Objects/Map/keys +browser-compat: javascript.builtins.Map.keys +--- +<div>{{JSRef}}</div> + +<p><code><strong>keys()</strong></code> 返回一个引用的 <code><strong>Iterator</strong></code> 对象。它包含按照顺序插入 <code>Map</code> 对象中每个元素的key值。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-keys.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><code><em>myMap</em>.keys()</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>一个存在引用关系的 {{jsxref("Map")}} iterator 对象.</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用_keys">使用 <code>keys()</code></h3> + +<pre class="brush:js notranslate">var myMap = new Map(); +myMap.set("0", "foo"); +myMap.set(1, "bar"); +myMap.set({}, "baz"); + +var mapIter = myMap.keys(); + +console.log(mapIter.next().value); // "0" +console.log(mapIter.next().value); // 1 +console.log(mapIter.next().value); // Object +</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('ES2015', '#sec-map.prototype.keys', 'Map.prototype.keys')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.keys', 'Map.prototype.keys')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Map.keys")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Map.prototype.entries()")}}</li> + <li>{{jsxref("Map.prototype.values()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html new file mode 100644 index 0000000000..e0966fcec7 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/map/index.html @@ -0,0 +1,65 @@ +--- +title: Map() 构造函数 +slug: Web/JavaScript/Reference/Global_Objects/Map/Map +tags: + - Constructor + - JavaScript + - Map + - Reference + - Polyfill +translation_of: Web/JavaScript/Reference/Global_Objects/Map/Map +original_slug: Web/JavaScript/Reference/Global_Objects/Map/Map +browser-compat: javascript.builtins.Map.Map +--- +<div>{{JSRef}}</div> + +<p><strong><code>Map()</code> 构造函数</strong> 创建 {{jsxref("Map")}} 对象.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">new Map([<var>iterable</var>])</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code><var>iterable</var></code></dt> + <dd>Iterable 可以是一个{{jsxref("Array", "数组")}}或者其他 <a href="/zh-CN/docs/Web/JavaScript/Guide/iterable">iterable</a> 对象,其元素为键值对(两个元素的数组,例如: [[ 1, 'one' ],[ 2, 'two' ]])。 每个键值对都会添加到新的 Map。<code>null</code> 会被当做 <code>undefined。</code></dd> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">let myMap = new Map([ + [1, 'one'], + [2, 'two'], + [3, 'three'], +]) +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-map-constructor', 'Map constructor')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("javascript.builtins.Map.Map")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html new file mode 100644 index 0000000000..d4fd287926 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/set/index.html @@ -0,0 +1,98 @@ +--- +title: Map.prototype.set() +slug: Web/JavaScript/Reference/Global_Objects/Map/set +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Map/set +original_slug: Web/JavaScript/Reference/Global_Objects/Map/set +browser-compat: javascript.builtins.Map.set +--- +<p id="sect1">{{JSRef}}</p> + +<p><code><strong>set()</strong></code> 方法为 <code>Map</code> 对象添加或更新一个指定了键(<code>key</code>)和值(<code>value</code>)的(新)键值对。</p> + +<div>{{EmbedInteractiveExample("pages/js/map-prototype-set.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>.set(key, value);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>要添加至相应 <code>Map</code> 对象的元素的键。</dd> + <dt>value</dt> + <dd>要添加至相应 <code>Map</code> 对象的元素的值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>Map</code> 对象</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_set_方法">使用 <code>set</code> 方法</h3> + +<pre class="brush: js">var myMap = new Map(); + +// 将一个新元素添加到 Map 对象 +myMap.set("bar", "foo"); +myMap.set(1, "foobar"); + +// 在Map对象中更新某个元素的值 +myMap.set("bar", "baz"); +</pre> + +<h3 id="链式使用_set_方法">链式使用 <code>set</code> 方法</h3> + +<p>因为 Set() 方法返回 Map 对象本身,所以你可以像下面这样链式调用它:</p> + +<pre class="brush: js">// Add new elements to the map with chaining. +myMap.set('bar', 'foo') + .set(1, 'foobar') + .set(2, 'baz'); +</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('ES2015', '#sec-map.prototype.set', 'Map.prototype.set')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.set', 'Map.prototype.set')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Map.set")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Map.prototype.get()")}}</li> + <li>{{jsxref("Map.prototype.has()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html new file mode 100644 index 0000000000..97a5d475c3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/size/index.html @@ -0,0 +1,79 @@ +--- +title: Map.prototype.size +slug: Web/JavaScript/Reference/Global_Objects/Map/size +tags: + - ECMAScript 2015 + - JavaScript + - Map + - Property +translation_of: Web/JavaScript/Reference/Global_Objects/Map/size +original_slug: Web/JavaScript/Reference/Global_Objects/Map/size +browser-compat: javascript.builtins.Map.size +--- +<div>{{JSRef}}<br> + </div> + +<p><code><strong>size</strong></code> 是可访问属性,用于返回 一个{{jsxref("Map")}} 对象的成员数量。</p> + +<p>{{EmbedInteractiveExample("pages/js/map-prototype-size.html")}}</p> + +<p>这个示例源码保存在GitHub:<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a>。如果你想贡献代码,修改后在GitHub上发推送请求给我们。</p> + +<p> </p> + +<h2 id="描述">描述</h2> + +<p>size 属性的值是一个整数,表示 Map 对象有多少个键值对。size 是只读属性,用set 方法修改size返回 undefined,即不能改变它的值。</p> + +<h2 id="示例">示例</h2> + +<pre class="brush:js">var myMap = new Map(); +myMap.set("a", "alpha"); +myMap.set("b", "beta"); +myMap.set("g", "gamma"); + +myMap.size // 3 +</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>{{SpecName('ES6', '#sec-get-map.prototype.size', 'Map.prototype.size')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-get-map.prototype.size', 'Map.prototype.size')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<p>此页的兼容性表格请查阅:<a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> <br> + 如果你想更新数据,请在GitHub上给我们发推送请求。</p> + +<div class="blockIndicator warning"> +<p><strong>异常提醒</strong></p> +</div> + +<ul> + <li>在JavaScript中直接书写Map.prototype.size可能得到以下异常:<br> + <code>Uncaught TypeError: Method get Map.prototype.size called on incompatible receiver #<Map></code><br> + 异常原因查看:<a href="/zh-CN/docs/">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Called_on_incompatible_type</a></li> +</ul> + +<h2 id="相关阅读">相关阅读</h2> + +<ul> + <li>{{jsxref("Map")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html b/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html new file mode 100644 index 0000000000..5c4aad2316 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/map/values/index.html @@ -0,0 +1,121 @@ +--- +title: Map.prototype.values() +slug: Web/JavaScript/Reference/Global_Objects/Map/values +tags: + - ECMAScript 2015 + - Iterator + - JavaScript + - Map + - Method + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Map/values +original_slug: Web/JavaScript/Reference/Global_Objects/Map/values +browser-compat: javascript.builtins.Map.values +--- +<div><code><strong>values()</strong></code> 方法返回一个新的Iterator对象。它包含按顺序插入Map对象中每个元素的value值。</div> + +<div> </div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>myMap</em>.values()</code></pre> + +<h3 id="返回值">返回值</h3> + +<p>一个新的 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Map" title="此页面仍未被本地化, 期待您的翻译!"><code>Map</code></a> 可迭代对象.</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用_values()"><code>使用 values()</code></h3> + +<pre class="brush:js">var myMap = new Map(); +myMap.set("0", "foo"); +myMap.set(1, "bar"); +myMap.set({}, "baz"); + +var mapIter = myMap.values(); + +console.log(mapIter.next().value); // "foo" +console.log(mapIter.next().value); // "bar" +console.log(mapIter.next().value); // "baz"</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-map.prototype.values', 'Map.prototype.values')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-map.prototype.values', 'Map.prototype.values')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{CompatibilityTable}}</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>{{ CompatGeckoDesktop("20") }}</td> + <td>{{CompatNo}}</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>{{CompatNo}}</td> + <td>38</td> + <td>{{ CompatGeckoMobile("20") }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="查看">查看</h2> + +<ul> + <li>{{jsxref("Map.prototype.entries()")}}</li> + <li>{{jsxref("Map.prototype.keys()")}}</li> +</ul> |