--- title: WeakSet.prototype.add() slug: Web/JavaScript/Reference/Global_Objects/WeakSet/add tags: - ECMAScript 2015 - JavaScript - WeakSet translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/add ---
add()
方法在 WeakSet
对象的最后一个元素后添加新的对象。
ws.add(value);
WeakSet
集合中。WeakSet
对象。
add
方法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 浏览器中
Specification | Status | Comment |
---|---|---|
{{SpecName('ES2015', '#sec-weakset.prototype.add', 'WeakSet.prototype.add')}} | {{Spec2('ES2015')}} | Initial definition. |
{{SpecName('ESDraft', '#sec-weakset.prototype.add', 'WeakSet.prototype.add')}} | {{Spec2('ESDraft')}} |