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/weakmap | |
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/weakmap')
7 files changed, 840 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/clear/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/clear/index.html new file mode 100644 index 0000000000..b2d51c64a1 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/clear/index.html @@ -0,0 +1,104 @@ +--- +title: WeakMap.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/clear +tags: + - JavaScript + - Method + - WeakMap + - clear() +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/clear +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p><code><strong>clear()</strong></code>用来从 <code>WeakMap对象中移除所有元素。</code>但不再是ECMAScript和它的实现部分。</p> + +<div class="warning"> +<p>Warning:</p> + +<p><img alt="WeakMap(no clear() any more).png" src="https://mdn.mozillademos.org/files/15608/WeakMap(no%20clear()%20any%20more).png" style="height: 490px; width: 735px;"></p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>wm</em>.clear();</pre> + +<h2 id="示例">示例</h2> + +<h3 id="使用_clear_方法">使用 <code>clear</code> 方法</h3> + +<pre class="brush: js;highlight:[10] example-bad">var wm = new WeakMap(); +var obj = {}; + +wm.set(obj, "foo"); +wm.set(window, "bar"); + +wm.has(obj); // true +wm.has(window); // true + +wm.clear(); + +wm.has(obj) // false +wm.has(window) // false +</pre> + +<h2 id="规范">规范</h2> + +<p>当前版本或者起草中没有这个方法,这个方法在版本 28(2014 年 10 月 14) 之前是 ECMAScript 6 起草规范的一部分,但是在起草之后的版本中被移除了。它不在是最终标准的一部分了 。</p> + +<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>36</td> + <td>{{CompatNo}} [1]</td> + <td>11</td> + <td>23</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>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>{{CompatNo}} [1]</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] The <code>clear()</code> 曾经在高于 20 版本低于 45 版本之间被支持。</p> + +<h2 id="相近">相近</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/delete/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/delete/index.html new file mode 100644 index 0000000000..337563ecd3 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/delete/index.html @@ -0,0 +1,108 @@ +--- +title: WeakMap.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/delete +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/delete +--- +<div>{{JSRef("Global_Objects", "WeakMap")}}</div> + +<h2 id="Summary" name="Summary">概述</h2> + +<p><code><strong>delete()</strong></code> 方法可以从一个 <code>WeakMap</code> 对象中删除指定的元素。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><em>wm</em>.delete(key);</code></pre> + +<h3 id="Parameters参数">Parameters参数</h3> + +<dl> + <dt>key</dt> + <dd>需要删除的元素的键</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>如果成功删除,返回 <code>true</code>,否则返回 <code>false</code>。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<pre class="brush: js;">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.delete(window); // 返回 true,表示删除成功。 + +wm.has(window); // 返回 false,因为 window 对象已经被删除了。 +</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-weakmap.prototype.delete', 'WeakMap.prototype.delete')}}</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>Feature</th> + <th>Chrome</th> + <th>Firefox (SpiderMonkey)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}[1]</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</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>Firefox Mobile (SpiderMonkey)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/get/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/get/index.html new file mode 100644 index 0000000000..5dd596c3ff --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/get/index.html @@ -0,0 +1,120 @@ +--- +title: WeakMap.prototype.get() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/get +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/get +--- +<div>{{JSRef}}</div> + +<p><code><strong>get()</strong></code> 方法返回 <code>WeakMap</code> 指定的元素。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>wm</em>.get(key);</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>必须。 想要从 <code>WeakMap </code>获取的元素的键。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<h3 id="返回与指定键相关联的值,如果_WeakMap_对象找不到这个键则返回_undefined。"><span style="font-size: 14px; line-height: 1.5;">返回与指定键相关联的值,如果 </span><code style="font-size: 14px; font-style: normal; font-weight: normal; line-height: 1.5;">WeakMap</code><span style="font-size: 14px; line-height: 1.5;"> 对象找不到这个键则返回 </span><code style="font-size: 14px; font-style: normal; font-weight: normal; line-height: 1.5;">undefined</code><span style="font-size: 14px; line-height: 1.5;">。</span></h3> + +<h2 id="例子">例子</h2> + +<h3 id="使用_get_方法">使用 <code>get </code>方法<code> </code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.get(window); // 返回 "foo". +wm.get("baz"); // 返回 undefined. +</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-weakmap.prototype.get', 'WeakMap.prototype.get')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-weakmap.prototype.get', 'WeakMap.prototype.get')}}</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>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</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>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>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Firefox_中的注意事项">Firefox 中的注意事项</h2> + +<ul> + <li>Prior to SpiderMonkey 38 {{geckoRelease(38)}}, this method threw a {{jsxref("TypeError")}} when the key parameter was not an object. However, the latest ES6 standard specifies to return <code>undefined</code> instead. Furthermore, <code>WeakMap.prototype.get</code> accepted an optional second argument as a fallback value, which is not part of the standard. Both non-standard behaviors are removed in version 38 and higher ({{bug(1127827)}}).</li> +</ul> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.set()")}}</li> + <li>{{jsxref("WeakMap.has()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/has/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/has/index.html new file mode 100644 index 0000000000..2c411d429d --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/has/index.html @@ -0,0 +1,123 @@ +--- +title: WeakMap.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/has +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/has +--- +<div>{{JSRef}}</div> + +<p> <code><strong>has()</strong></code> 方法根据WeakMap对象的元素中是否存在key键返回一个boolean值。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><code><em>wm</em>.has(key);</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt>key</dt> + <dd>必须的。用来检测WeakMap对象中是否存在元素的键为key。</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<dl> + <dt>Boolean</dt> + <dd>如果指定的key存在于某个元素中则返回true,否则返回flase。</dd> +</dl> + +<h2 id="例子">例子</h2> + +<h3 id="使用_has方法">使用 <code>has方法</code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.has(window); // returns true +wm.has("baz"); // returns 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('ES6', '#sec-weakmap.prototype.has', 'WeakMap.prototype.has')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-weakmap.prototype.has', 'WeakMap.prototype.has')}}</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>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</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>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>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Firefox-特有说明">Firefox-特有说明</h2> + +<ul> + <li>直到SpiderMonkey 38 {{geckoRelease(38)}}, 这个方法在key参数不是一个对象时会抛出 {{jsxref("TypeError")}}。它已经从版本38后按照最新版本的ES6标准修复 ({{bug(1127827)}}).</li> +</ul> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.prototype.set()")}}</li> + <li>{{jsxref("WeakMap.prototype.get()")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/index.html new file mode 100644 index 0000000000..936763d12f --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/index.html @@ -0,0 +1,159 @@ +--- +title: WeakMap +slug: Web/JavaScript/Reference/Global_Objects/WeakMap +tags: + - ECMAScript 2015 + - JavaScript + - WeakMap +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap +--- +<div>{{JSRef}}</div> + +<p><strong><code>WeakMap</code></strong> 对象是一组键/值对的集合,其中的键是弱引用的。其键必须是对象,而值可以是任意的。</p> + +<p>你可以从这里了解更多关于<code>WeakMap</code>的内容:{{SectionOnPage("/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections", "WeakMap对象")}}.</p> + +<h2 id="语法">语法</h2> + +<pre>new WeakMap([iterable])</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>iterable</code></dt> + <dd>Iterable 是一个数组(二元数组)或者其他可迭代的且其元素是键值对的对象。每个键值对会被加到新的 WeakMap 里。null 会被当做 undefined。</dd> +</dl> + +<h2 id="描述">描述</h2> + +<p>WeakMap 的 key 只能是 <code>Object</code> 类型。 {{Glossary("Primitive", "原始数据类型")}} 是不能作为 key 的(比如 {{jsxref("Symbol")}})。</p> + +<h3 id="Why_WeakMap">Why <em>WeakMap</em>?</h3> + +<p>在 JavaScript 里,map API 可以通过使其四个 API 方法共用两个数组(一个存放键,一个存放值)来实现。给这种 map 设置值时会同时将键和值添加到这两个数组的末尾。从而使得键和值的索引在两个数组中相对应。当从该 map 取值的时候,需要遍历所有的键,然后使用索引从存储值的数组中检索出相应的值。</p> + +<p>但这样的实现会有两个很大的缺点,首先赋值和搜索操作都是 O(n) 的时间复杂度( n 是键值对的个数),因为这两个操作都需要遍历全部整个数组来进行匹配。另外一个缺点是可能会导致内存泄漏,因为数组会一直引用着每个键和值。这种引用使得垃圾回收算法不能回收处理他们,即使没有其他任何引用存在了。</p> + +<p>相比之下,原生的 WeakMap 持有的是每个键对象的“弱引用”,这意味着在没有其他引用存在时垃圾回收能正确进行。原生 WeakMap 的结构是特殊且有效的,其用于映射的 key 只有在其没有被回收时才是有效的。</p> + +<p>正由于这样的弱引用,<code>WeakMap</code> 的 key 是不可枚举的 (没有方法能给出所有的 key)。如果key 是可枚举的话,其列表将会受垃圾回收机制的影响,从而得到不确定的结果。因此,如果你想要这种类型对象的 key 值的列表,你应该使用 {{jsxref("Map")}}。</p> + +<p>基本上,如果你要往对象上添加数据,又不想干扰垃圾回收机制,就可以使用 WeakMap。</p> + +<h2 id="属性">属性</h2> + +<dl> + <dt><code>WeakMap.length</code></dt> + <dd><code>length</code> 属性的值为 0。</dd> + <dt>{{jsxref("WeakMap.prototype")}}</dt> + <dd><code>WeakMap</code> 构造器的原型。 允许添加属性到所有的 WeakMap 对象。</dd> +</dl> + +<h2 id="WeakMap_实例"><code>WeakMap</code> 实例</h2> + +<p>所有 <code>WeakMap</code> 实例继承自 {{jsxref("WeakMap.prototype")}}.</p> + +<h3 id="属性_2">属性</h3> + +<p>{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','属性')}}</p> + +<h3 id="方法">方法</h3> + +<p>{{page('zh-CN/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','方法')}}</p> + +<h2 id="示例">示例</h2> + +<h3 id="使用_WeakMap">使用 <code>WeakMap</code></h3> + +<pre class="brush: js">const wm1 = new WeakMap(), + wm2 = new WeakMap(), + wm3 = new WeakMap(); +const o1 = {}, + o2 = function(){}, + o3 = window; + +wm1.set(o1, 37); +wm1.set(o2, "azerty"); +wm2.set(o1, o2); // value可以是任意值,包括一个对象或一个函数 +wm2.set(o3, undefined); +wm2.set(wm1, wm2); // 键和值可以是任意对象,甚至另外一个WeakMap对象 + +wm1.get(o2); // "azerty" +wm2.get(o2); // undefined,wm2中没有o2这个键 +wm2.get(o3); // undefined,值就是undefined + +wm1.has(o2); // true +wm2.has(o2); // false +wm2.has(o3); // true (即使值是undefined) + +wm3.set(o1, 37); +wm3.get(o1); // 37 + +wm1.has(o1); // true +wm1.delete(o1); +wm1.has(o1); // false +</pre> + +<h3 id="实现一_个带有_.clear_方法的类_WeakMap_类">实现一 个带有 .clear() 方法的类 <code>WeakMap</code> 类</h3> + +<pre class="brush: js">class ClearableWeakMap { + constructor(init) { + this._wm = new WeakMap(init) + } + clear() { + this._wm = new WeakMap() + } + delete(k) { + return this._wm.delete(k) + } + get(k) { + return this._wm.get(k) + } + has(k) { + return this._wm.has(k) + } + set(k, v) { + this._wm.set(k, v) + return this + } +}</pre> + +<h2 id="Specifications" name="Specifications">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap-objects', 'WeakMap')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-weakmap-objects', 'WeakMap')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<div>{{Compat("javascript.builtins.WeakMap")}}</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=547941">WeakMap bug at Mozilla</a></li> + <li><a href="http://fitzgeraldnick.com/weblog/53/">Hiding Implementation Details with ECMAScript 6 WeakMaps</a></li> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html new file mode 100644 index 0000000000..706b6d795c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/prototype/index.html @@ -0,0 +1,137 @@ +--- +title: WeakMap.prototype +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap +--- +<div>{{JSRef}}</div> + +<p><code><strong>WeakMap</strong></code><strong><code>.prototype</code></strong>属性表现为 {{jsxref("WeakMap")}}的构造器。</p> + +<div>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="描述">描述</h2> + +<p>{{jsxref("WeakMap")}} 实例从 {{jsxref("WeakMap.prototype")}}继承了所有属性。你可以在<code>WeakMap构造器中添加属性和方法,从而使得所有</code>实例中都有效。</p> + +<p><code>WeakMap.prototype</code> 本身只是一个普通的对象:</p> + +<pre class="brush: js">Object.prototype.toString.call(WeakMap.prototype); // "[object Object]"</pre> + +<h2 id="属性">属性</h2> + +<dl> + <dt><code>WeakMap.prototype.constructor</code></dt> + <dd>返回创建WeakMap实例的原型函数。 {{jsxref("WeakMap")}}函数是默认的。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{jsxref("WeakMap.delete", "WeakMap.prototype.delete(key)")}}</dt> + <dd>移除key的关联对象。执行后 <code>WeakMap.prototype.has(key)返回</code><code>false。</code></dd> + <dt>{{jsxref("WeakMap.get", "WeakMap.prototype.get(key)")}}</dt> + <dd>返回<code>key关联对象</code>, 或者 <code>undefined</code>(没有key关联对象时)。</dd> + <dt>{{jsxref("WeakMap.has", "WeakMap.prototype.has(key)")}}</dt> + <dd>根据是否有key关联对象返回一个Boolean值。</dd> + <dt>{{jsxref("WeakMap.set", "WeakMap.prototype.set(key, value)")}}</dt> + <dd>在WeakMap中设置一组key关联对象,返回这个 <code>WeakMap</code>对象。</dd> + <dt><s class="obsoleteElement">{{jsxref("WeakMap.prototype.clear()")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">从<code>WeakMap中移除所有的</code> key/value 。 注意,该方法已弃用,但可以通过创建一个空的WeakMap并替换原对象来实现 (参看 {{jsxref("WeakMap")}}的后半部分)</s></dd> +</dl> + +<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-weakmap.prototype', 'WeakMap.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-weakmap.prototype', 'WeakMap.prototype')}}</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>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + <tr> + <td>Ordinary object</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</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>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + <tr> + <td>Ordinary object</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li>{{jsxref("Map.prototype")}}</li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/weakmap/set/index.html b/files/zh-cn/web/javascript/reference/global_objects/weakmap/set/index.html new file mode 100644 index 0000000000..9643d66bfd --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/weakmap/set/index.html @@ -0,0 +1,89 @@ +--- +title: WeakMap.prototype.set() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/set +tags: + - JavaScript + - Method + - WeakMap +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/set +--- +<div>{{JSRef}}</div> + +<p><code><strong>set()</strong></code> 方法根据指定的<code>key</code>和<code>value在</code> <code>WeakMap</code>对象中添加新/更新元素。</p> + +<div>{{EmbedInteractiveExample("pages/js/weakmap-prototype-set.html")}}</div> + + + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>wm</em>.set(key, value);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>key</dt> + <dd>必须的。必须是<code>对象</code>。是要在<code>WeakMap</code> 对象中添加元素的key部分。</dd> + <dt>value</dt> + <dd>必须的。任意的值。是要在<code>WeakMap</code> 对象中添加/元素的value部分。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>该<code>WeakMap</code>对象</p> + +<h2 id="例子">例子</h2> + +<h3 id="使用set方法"><code>使用set方法</code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +var obj = {}; + +// Add new elements to the WeakMap +wm.set(obj, "foo").set(window, "bar"); // chainable + +// Update an element in the WeakMap +wm.set(obj, "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('ES6', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("javascript.builtins.WeakMap.set")}}</p> + +<h2 id="Firefox-特殊说明">Firefox-特殊说明</h2> + +<ul> + <li>直到Firefox 33 {{geckoRelease("33")}}, <code>WeakMap.prototype.set</code> returned <code>undefined</code> 而不支持联式。它已经修复 ({{bug(1031632)}}). 这样的实现也存在于 Chrome/v8 (<a href="https://code.google.com/p/v8/issues/detail?id=3410">issue</a>).</li> +</ul> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.prototype.get()")}}</li> + <li>{{jsxref("WeakMap.prototype.has()")}}</li> +</ul> |