119 lines
4.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div> </div>
<p><code><strong>add()</strong></code> 方法用来向一个 <code>Set</code> 对象的末尾添加一个指定的值。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox"><code><em>mySet</em>.add(value);</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt>value</dt>
<dd>必需。需要添加到 <code>Set </code>对象的元素的值。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p><code>Set</code> 对象本身</p>
<p>注意:不能添加重复的值</p>
<h2 id="Examples" name="Examples">示例</h2>
<pre class="brush: js">var mySet = new Set();
mySet.add(1);
mySet.add(5).add("some text"); // 可以链式调用
console.log(mySet);
// Set [1, 5, "some text"]
mySet.add(5).add(1);
console.log(mySet);
//Set [1, 5] // 重复的值没有被添加进去</pre>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<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.add" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Set.prototype.add</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-set.prototype.add" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Set.prototype.add</small></a></td>
<td><span class="spec-Draft">Draft</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>38</td>
<td><a href="/en-US/Firefox/Releases/13" title="Released on 2012-06-05.">13.0</a> (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>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>13.0 (13.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>低于 Firefox 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30)的版本,<code>Set.prototype.add</code> 会返回<code>undefined 并且不可链式调用。</code> 该问题(<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1031632" rel="noopener" title="FIXED: Map.prototype.set, WeakMap.prototype.set and Set.prototype.add should be chainable">bug 1031632</a>)已修复。Chrome/v8 中也有该问题 (<a class="external" href="https://code.google.com/p/v8/issues/detail?id=3410" rel="noopener">issue</a>).</li>
</ul>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Set" title="Set 对象允许你存储任何类型的唯一值无论是原始值或者是对象引用。"><code>Set</code></a></li>
<li><a href="Reference/Global_Objects/Set/delete" title="delete() 方法可以从一个 Set 对象中删除指定的元素。"><code>Set.prototype.delete()</code></a></li>
<li><a href="Reference/Global_Objects/Set/has" title="has() 方法返回一个布尔值来指示对应的值value是否存在Set对象中。"><code>Set.prototype.has()</code></a></li>
</ul>
</article>