Advertisement
728x90
Below are the list of methods available on WeakMap,
1. set(key, value): Sets the value for the key in the WeakMap object. Returns the WeakMap object.
2. delete(key): Removes any value associated to the key.
3. has(key): Returns a Boolean asserting whether a value has been associated to the key in the WeakMap object or not.
4. get(key): Returns the value associated to the key, or undefined if there is none.
Let's see the functionality of all the above methods in an example,
javascript
1var weakMapObject = new WeakMap();
2
3var firstObject = {};
4
5var secondObject = {};
6// set(key, value)
7
8weakMapObject.set(firstObject, "John");
9
10weakMapObject.set(secondObject, 100);
11
12console.log(weakMapObject.has(firstObject)); //true
13
14console.log(weakMapObject.get(firstObject)); // John
15
16weakMapObject.delete(secondObject);Advertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 39
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
209of476