--- title: WeakSet.prototype.add() slug: Web/JavaScript/Reference/Global_Objects/WeakSet/add tags: - ECMAScript 2015 - JavaScript - Method - Prototype - WeakSet translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/add --- <div>{{JSRef}}</div> <p><strong><code>add()</code></strong> メソッドは、新しい要素を <code>WeakSet</code> オブジェクトの最後に追加します。</p> <div>{{EmbedInteractiveExample("pages/js/weakset-prototype-add.html", "taller")}}</div> <div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div> <h2 id="Syntax" name="Syntax">構文</h2> <pre class="syntaxbox notranslate"><var>ws</var>.add(<var>value</var>);</pre> <h3 id="Syntax" name="Syntax">引数</h3> <dl> <dt><code><var>value</var></code></dt> <dd>必須。 <code>WeakSet</code> コレクションに追加する要素の値です。</dd> </dl> <h3 id="Return_value" name="Return_value">返値</h3> <p><code>WeakSet</code> オブジェクトです。</p> <h2 id="Examples" name="Examples">例</h2> <h3 id="Using_add" name="Using_add">add() の使用</h3> <pre class="brush: js notranslate">var ws = new WeakSet(); ws.add(window); //window オブジェクトを WeakSet へ追加 ws.has(window); // true // WeakSet は引数としてオブジェクトのみを取ります。 ws.add(1); // 結果は "TypeError: Invalid value used in weak set" (Chrome) // "TypeError: 1 is not a non-null object" (Firefox)</pre> <h2 id="Specifications" name="Specifications">仕様書</h2> <table class="standard-table"> <thead> <tr> <th scope="col">仕様書</th> </tr> </thead> <tbody> <tr> <td>{{SpecName('ESDraft', '#sec-weakset.prototype.add', 'WeakSet.prototype.add')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2> <p>{{Compat("javascript.builtins.WeakSet.add")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{jsxref("WeakSet")}}</li> <li>{{jsxref("WeakSet.prototype.delete()")}}</li> <li>{{jsxref("WeakSet.prototype.has()")}}</li> </ul>