mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 23:44:06 +08:00
141 lines
7.7 KiB
HTML
141 lines
7.7 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<div> </div>
|
||
<p><strong><code>WeakSet</code></strong> 对象允许你将<em>弱保持对象</em>存储在一个集合中。</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript"> new WeakSet([iterable]);</code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt>iterable</dt>
|
||
<dd>如果传入一个<a href="Reference/Statements/for...of">可迭代对象</a>作为参数, 则该对象的所有迭代值都会被自动添加进生成的 <code>WeakSet</code> 对象中.</dd>
|
||
</dl>
|
||
<h2 id="描述">描述</h2>
|
||
<p><code>WeakSet</code> 对象是一些对象值的集合, 并且其中的每个对象值都只能出现一次.</p>
|
||
<p>它和 <a href="Reference/Global_Objects/Set" title="Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。"><code>Set</code></a> 对象的区别有两点:</p>
|
||
<ul>
|
||
<li><code>WeakSet</code> 对象中只能存放对象引用, 不能存放值, 而 <code>Set</code> 对象都可以.</li>
|
||
<li><code>WeakSet</code> 对象中存储的对象值都是被弱引用的, 如果没有其他的变量或属性引用这个对象值, 则这个对象值会被当成垃圾回收掉. 正因为这样, <code>WeakSet</code> 对象是无法被枚举的, 没有办法拿到它包含的所有元素.</li>
|
||
</ul>
|
||
<h2 id="Properties" name="Properties">属性</h2>
|
||
<dl>
|
||
<dt><code>WeakSet.length</code></dt>
|
||
<dd><code>length</code> 属性的值为 0.</dd>
|
||
<dt><a href="Reference/Global_Objects/WeakSet/prototype" title="The WeakSet.prototype property represents the prototype for the WeakSet constructor."><code>WeakSet.prototype</code></a></dt>
|
||
<dd><span><code>WeakSet</code> 实例的所有继承属性和继承方法都在该对象上.</span></dd>
|
||
</dl>
|
||
<h2 id="Boolean_instances" name="Boolean_instances"><code>WeakSet</code> 实例</h2>
|
||
<p>所有 <code>WeakSet</code> 实例都继承自 <a href="Reference/Global_Objects/WeakSet/prototype" title="The WeakSet.prototype property represents the prototype for the WeakSet constructor."><code>WeakSet.prototype</code></a>.</p>
|
||
<h3 id="属性">属性</h3>
|
||
<p></p><dl>
|
||
<dt><code>WeakSet.prototype.constructor</code></dt>
|
||
<dd>返回构造函数即 <a href="Reference/Global_Objects/WeakSet" title="WeakSet 对象允许你将弱保持对象存储在一个集合中。"><code>WeakSet</code></a> 本身.</dd>
|
||
</dl><p></p>
|
||
<h3 id="方法">方法</h3>
|
||
<p></p><dl>
|
||
<dt><a href="Reference/Global_Objects/WeakSet/add" title="add() 方法在 WeakSet 对象的最后一个元素后添加新的对象。"><code>WeakSet.prototype.add(value)</code></a></dt>
|
||
<dd> 在<span style="line-height: 1.5;">该 </span><code style="font-size: 14px;">WeakSet</code><span style="line-height: 1.5;"> 对象中添加一个新元素 <code>value</code>.</span></dd>
|
||
<dt><a href="Reference/Global_Objects/WeakSet/clear" title="clear() 方法用于删除 WeakSet 对象的所有元素,但是已经不是 ECMAScript 的一部分了。"><code>WeakSet.prototype.clear()</code></a></dt>
|
||
<dd>清空该 <code>WeakSet</code> 对象中的所有元素.</dd>
|
||
<dt><a href="Reference/Global_Objects/WeakSet/delete" title="delete() 方法从 WeakSet 对象中移除指定的元素。"><code>WeakSet.prototype.delete(value)</code></a></dt>
|
||
<dd>从<span style="line-height: 1.5;">该 </span><code style="font-size: 14px;">WeakSet</code><span style="line-height: 1.5;"> 对象中删除</span><span style="line-height: 1.5;"> </span><code style="font-size: 14px;">value </code><span style="line-height: 1.5;">这个元素, 之后 </span><code style="font-size: 14px;">WeakSet.prototype.has(value)</code><span style="line-height: 1.5;"> 方法便会返回 </span><code style="font-size: 14px;">false</code><span style="line-height: 1.5;">.</span></dd>
|
||
<dt><a href="Reference/Global_Objects/WeakSet/has" title="has() 方法根据 WeakSet 是否存在相应对象返回布尔值。"><code>WeakSet.prototype.has(value)</code></a></dt>
|
||
<dd>返回一个布尔值, 表示给定的值 <code>value</code> 是否存在于这个 <code>WeakSet</code> 中.</dd>
|
||
</dl><p></p>
|
||
<h2 id="示例">示例</h2>
|
||
<h3 id="例1_使用_WeakSet">例1: 使用 <code>WeakSet</code></h3>
|
||
<pre><code class="language-javascript">var ws = new WeakSet();
|
||
var obj = {};
|
||
var foo = {};
|
||
|
||
ws.add(window);
|
||
ws.add(obj);
|
||
|
||
ws.has(window); // true
|
||
ws.has(foo); // false, 对象 foo 并没有被添加进 ws 中
|
||
|
||
ws.delete(window); // 从集合中删除 window 对象
|
||
ws.has(window); // false, window 对象已经被删除了
|
||
|
||
ws.clear(); // 清空整个 WeakSet 对象
|
||
</code></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><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-weakset-objects" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">WeakSet</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>无</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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td><span style="color: #f00;">未实现</span> <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=792439" rel="noopener" title="FIXED: Implement ES6 WeakSet">bug 792439</a></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></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><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span> <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=792439" rel="noopener" title="FIXED: Implement ES6 WeakSet">bug 792439</a></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h3 id="Chrome_备注">Chrome 备注</h3>
|
||
<ul>
|
||
<li>需在 <code>chrome://flags 中开启</code> “启用实验性 JavaScript” 才能使用该特性.</li>
|
||
</ul>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Set" title="Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。"><code>Set</code></a></li>
|
||
<li><a href="Reference/WeakMap" title="此页面仍未被本地化, 期待您的翻译!"><code>WeakMap</code></a></li>
|
||
</ul>
|
||
</article> |