• JavaScript Video Tutorials

JavaScript - WeakMap() Constructor



In JavaScript, the WeakMap is a built-in object that allows us to store key-value pairs in a similar way to a regular Map, but with a few key differences −

  • Keys − In WeakMap, keys can only be objects, not primitive values like string or numbers.

  • Size − WeakMaps do not have a size property or forEach() method to get the number of items in a WeakMap or iterate over its elements.

  • Clear − WeakMaps do not have a clear() method to get remove all the elements from a WeakMap at once. However, we can remove individual elements by deleting their key from the WeakMap using delete() method.

  • Not enumerable − The keys of a WeakMap are not enumerable, so we cannot use methods like keys(), values(), or entries() to get the keys or values of a WeakMap.

  • Garbage collection − WeakMap objects are used to store weakly held objects, Which means that the keys and values can be garbage collected if they are no longer referenced anywhere else in your code.

The WeakMap() constructor creates a new WeakMap object. It can take an optional iterable argument, which is an iterable object whose elements are key-value pairs. If provided, the key-value pairs are added to the new WeakMap object.

The WeakMap() constructor can only be constructed with new keyword. If we attempt to invoke it without 'new', throws a TypeError.

Syntax

Following is the syntax of JavaScript WeakMap() constructor −

new WeakMap()
new WeakMap(iterable)

Parameters

This constructor accepts an optional parameter. The same is described below −

  • iterable (optional) − It can be any iterable object that contains array-like objects with two elements: the key and the value. If no iterable argument is provided, an empty WeakMap object is created.

Return Value

The return value of the WeakMap() constructor is a new WeakMap object.

Example 1

In the following example, we are creating an empty WeakMap object using the JavaScript WeakMap() constructor −

<html>
<body>
   <script>
      const weakMap = new WeakMap();
      if(weakMap){
         document.write("Weakmap object created successfully...")
      }
   </script>
</body>
</html>

Output

If we execute the above program, an empty WeakMap object will be created.

Example 2

We can also pass an optional iterable object as a parameter to the WeakMap() constructor as shown below −

<html>
<body>
   <script>
      let key1 = {id: 1};
      let key2 = {id: 2};
      let key3 = {id: 3};

      const weakMap = new WeakMap([
         [key1, 'apple'],
         [key2, 'banana'],
         [key3, 'cherry']
      ]);
      document.write(weakMap.get(key1) + ", " +   weakMap.get(key2) + ", " + weakMap.get(key3));
   </script>
</body>
</html>

Output

The above program returns the values associated with their respective keys.

Example 3

We can also add key-value pairs to a WeakMap object after it has been created using the set() method as shown in the following example −

<html>
<body>
   <script>
      let weakMap = new WeakMap();
      let key1 = {};
      let key2 = {};
      let key3 = {};

      weakMap.set(key1, "apple");
      weakMap.set(key2, "banana");
      weakMap.set(key3, "cherry");
      document.write(weakMap.get(key1) + ", " +   weakMap.get(key2) + ", " + weakMap.get(key3));
   </script>
</body>
</html>

Output

The "weakMap" object will contain the provided three key-value pairs as elements. Also, if we execute the program, we can see the "values" associated with their respective keys.

Example 4

We can access the value associated with a key in a WeakMap object using the get() method as shown in the below example −

<html>
<body>
   <script>
      let weakMap = new WeakMap();
      let id1 = {id: 1};
      let id2 = {id: 2};
      let id3 = {id: 3};

      weakMap.set(id1, "apple");
      weakMap.set(id2, "banana");
      weakMap.set(id3, "cherry");

      document.write(weakMap.get(id1), "<br>");
      document.write(weakMap.get(id2), "<br>");
      document.write(weakMap.get(id3));
   </script>
</body>
</html>

Output

The above program prints the "values" associated with the keys 'id1', 'id2', and 'id3' in the WeakMap object.

Example 5

Here, we are using the delete() method to delete a key-value pair from a WeakMap object −

<html>
<body>
   <script>
      let weakMap = new WeakMap();
      let id1 = {id :1};
      let id2 = {id :2};
      let id3 = {id :3};

      weakMap.set(id1, "apple");
      weakMap.set(id2, "banana");
      weakMap.set(id3, "cherry");

      document.write(weakMap.delete(id1), "<br>");
      document.write(weakMap.delete(id2));
   </script>
</body>
</html>

Output

The above program removes the key-value pair associated with the key 'id1' and 'id2' from the WeakMap object.

Advertisements