Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptWhat is the use of a WeakSet object in JavaScript ?

What is the use of a WeakSet object in JavaScript ?

Introduction: The JavaScript WeakSet object is a sort of collection that allows us to store items that are only loosely held. WeakSet, unlike Set, is just a collection of items. It does not include arbitrary values. It has the same features as a set, in that it does not hold duplicates. The main distinction between a WeakSet and a set is that a WeakSet is a collection of objects rather than values of a certain type. It supports add, has, and delete like Set, but not size, keys(), or iterations.

Syntax:

new WeakSet([iterable])  

Parameter:

  • iterable: It is an iterable object, the elements of which will be added to a new WeakSet.

Advantages of using WeakSet object:

  • The contents of a WeakSet can be garbage collected.
  • Possibility of lowering memory utilization.
  • Useful for class branding

Features of WeakSet object:

  • A WeakSet object only includes unique items.
  • If there is no reference to a stored object in WeakSet, it is targeted for garbage collection.
  • The items in WeakSet are not enumerable. As a result, it does not provide any mechanism for obtaining the requested objects.
  • It supports add, has, and delete like Set, but not size, keys(), or iterations.

Methods used with WeakSet object:

  • add(value):  In this method, value is appended to the WeakSet object.
  • has(value): It returns a boolean indicating whether or not the value is present in the WeakSet object.
  • delete(value): This method removes a value from the WeakSet.  WeakSet.prototype.has(value) will then return false.

Example 1: In this example, we will use the WeakSet() constructor to build new WeakSets. This will generate a new WeakSet, which you can then use to store data in. When you use it to build a new WeakSet, you may supply an iterable containing value as an argument to it. To determine whether or not a given object exists in a WeakSet, use the has(value).

Javascript




const gfg = {},
neveropen = {};  
const obj = new WeakSet([gfg, neveropen]);
  
// Checking if gfg exists
console.log(obj.has(gfg));


Output:

true

Example 2: In this example, we will create a WeakSet object with the weakset constructor and then add values with the add function. Following that, we verified whether or not the object we added were present. The object was then deleted from the weakset using the delete method, and then we again verified that the objects are successfully deleted or not.

Javascript




const obj = new WeakSet();
const gfg = {};
const neveropen = {};
  
// gfg object added
obj.add(gfg);
console.log(obj.has(gfg)) // true
console.log(obj.has(neveropen)) // false
  
// gfg object removed
obj.delete(gfg);
console.log(obj.has(gfg)) // false


Output:

true
false
false

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments