JavaScript WeakSet is used to store a collection of objects. It adapts the same properties as that of a set i.e. does not store duplicates. The major difference between WeakSet and a set is that a WeakSet is a collection of objects and not values of some particular type.
Syntax:
weakSet.function();
Example: In this example we will use the JavaScript weakSet() method.
Javascript
const A = new WeakSet();
const object1 = {};
A.add(object1);
console.log(A.has(object1));
|
Output:
true
The complete list of JavaScript WeakSet is listed below:
JavaScript WeakSet Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.
Constructor |
Description |
Example |
weakSet() |
A constructor gets called when an object is created using the new keyword. |
|
JavaScript WeakSet Properties: A JavaScript property is a member of an object that associates a key with a value. There is no static property in the weakSet only instance property.
- Instance Properties: An instance property is a property that has a new copy for every new instance of the class.
Instance Property |
Description |
Example |
constructor |
Return the wekaMap constructor function for the object. |
|
JavaScript WeakSet Method: JavaScript methods are actions that can be performed on objects.
- Instance Method: If the method is called on an instance of a WeakSet then it is called an instance method of WeakSet.
Instance Method |
Description |
Example |
add() |
Add an object at the end of an object WeakSet. |
|
delete() |
Delete a specific element from a weakSet object. |
|
has() |
Return a boolean value indicating whether an object is present in a weakSet or not. that |
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks 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!
Last Updated :
31 May, 2023
Like Article
Save Article