Use of 'weakMap.has()' method in JavaScript?


The key-value pairs are stored using JavaScript WeakMap Objects. A WeakMap object differs from a Map object in that such objects should be weakly referred and "object" should be stored in the WeakMap object as a key. In comparison, you can add simple values like strings, booleans, symbols, and numbers to the Map objects.

WeakMap objects are stored loosely, therefore if the references to a particular key are dropped or the object is erased, the garbage collection will drop the WeakMap element after it has verified that the value is mapped to the relevant object.

The JavaScript built-in function weakMap.has() is utilized to return a boolean value indicating whether or not an element with such a specific key is existing in the weakmap object.

Syntax

Following is the syntax of has() method

weakMap.has(key)

Parameter

  • key − Required. The element's key, which is used to check the WeakMap object for its presence.

Return value

Boolean

If the key given matches an element in the WeakMap object, the function returns true; else, it returns false.

Example 1

Here is a simple example using the weakMap.has() technique.

<!DOCTYPE html> <html> <title>Use of 'weakMap.has()' method in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> function myFunc() { const myWeakMap = new WeakMap(); const key = {}; myWeakMap.set(key, 'tutorialspoint'); document.write(myWeakMap.has(key)); } myFunc(); </script> </body> </html>

Example 2

Let's look at a JavaScript example to understand how such a function operates −

<!DOCTYPE html> <html> <title>Use of 'weakMap.has()' method in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> const myWeakmap = new WeakMap(); const myKey = {}; myWeakmap.set(myKey, 'tutorialspoint'); document.write(myWeakmap.has(myKey)); </script> </body> </html>

Example 3

In this example let us understand, if the weakMap object's key "key1" not being set at the end, the output in this case is false.

<!DOCTYPE html> <html> <title>Use of 'weakMap.has()' method in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> // written to Create a WeakMap() object const myWeakMap = new WeakMap(); // written to Create a key "myKey" const myKey = {}; // written to test if the key is present // in the weakMap() object or not document.write(myWeakMap.has(myKey)); </script> </body> </html>

Example 4

In this example let us understand how a JavaScript WeakMap object's elements can be removed or deleted using the delete() method. The key you would like to remove from the "weakMapObject" would be passed as an input to the delete() method. The delete() method returns "true" if the supplied key is successfully deleted; else, it returns "false" if the defined key cannot be found in the weakMapObject

The " myObject" from the "weakmap" object was deleted in the example below using JavaScript's "delete()" method. But first, let's make the following objects:

In order to confirm that "myObject" still exists in weakmap both before and after the deletion, we will additionally use the WeakMap Object "has()" method:

<!DOCTYPE html> <html> <title>Use of 'weakMap.has()' method in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body style="text-align:center"> <script> let myWeakMap = new WeakMap(); let myObject = {student: "Steve Smith"}; myWeakMap.set(myObject, 'Welcome to tutorialspoint'); document.write(myWeakMap.has(myObject) +'<br>'); myWeakMap.delete(myObject); document.write(myWeakMap.has(myObject)); </script> </body> </html>

The output seen above indicates that we were successful in removing the " myObject" from the "weakmap" object. Since " myObject" was still available in the weakmap object at that time, the first has() function returned "true." After using the delete() function on the JavaScript Weakset Object, myObject was removed from the weakmap object and the second has() method returned "false."

In Brief

For recording key-value pairs with weak security, use the JavaScript WeakMap Object. Primitive values like strings, symbols, booleans, and numbers cannot be stored in a WeakMap object, unlike Maps. WeakMap objects can be used to store a DOM element's browser metadata or construct private variables in a class. Using appropriate examples, this article clarified JavaScript WeakMap Object.

Updated on: 23-Aug-2022

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements