Permalink
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (64 sloc) 2.4 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs ms.assetid caps.latest.revision author ms.author manager
WeakSet Object (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
f97e6e7c-d678-4e32-978e-d949a7cafa3a
5
mikejo5000
mikejo
ghogen

WeakSet Object (JavaScript)

A collection of unique objects that may be of any type.

Syntax

setObj = new WeakSet()  

Remarks

If you try to add a non-unique object to a WeakSet, the new object will not be added to the collection.

Unlike a Set, only objects may be added to the collection. Arbitrary values cannot be added to the collection.

In a WeakSet object, references to objects in the set are held 'weakly'. This means that WeakSet will not prevent a garbage collection from happening on the objects. When there are no references (other than WeakSet) to the objects, the garbage collector may collect the objects.

WeakSet (or WeakMap) may be helpful in some scenarios involving caching of objects or object metadata. For example, metadata for non-extensible objects may be stored in a WeakSet, or you may create a cache of user images using WeakSet.

Properties

The following table lists the properties of the WeakSet object.

Property Description
constructor Specifies the function that creates a set.
prototype Returns a reference to the prototype for a set.

Methods

The following table lists the methods of the WeakSet object.

Method Description
add Adds an element to a set.
delete Removes a specified element from a set.
has Returns true if the set contains a specified element.

Example

The following example shows how to add members to a set and then verify that they have been added.

var ws = new WeakSet();  
  
var str = new String("Thomas Jefferson");  
var num = new Number(1776);  
  
ws.add(str);  
ws.add(num);  
  
console.log(ws.has(str));  
console.log(ws.has(num));  
  
ws.delete(str);  
console.log(ws.has(str));  
  
// Output:  
// true  
// true  
// false  

Requirements

[!INCLUDEjsv12]