WeakSet Object in ES7 and TypeScript

The WeakSet object emerges as a unique entity designed specifically for storing weakly held objects. Unlike its counterpart, Set, WeakSet exclusively deals with objects, offering distinct advantages. This article delves into the intricacies of the JavaScript WeakSet object, exploring its syntax, characteristics, and methods.

JavaScript WeakSet Object in ES7

const myWeakSet = new WeakSet([iterable]);

Parameter

  • iterable: Represents an iterable object whose elements will be added to the new WeakSet.

Key Points

  1. Unique Objects: A WeakSet object exclusively contains unique objects. Unlike Set, it doesn't accommodate arbitrary values.
  2. Garbage Collection: In WeakSet, if there is no reference to a stored object, it becomes a candidate for garbage collection. This behavior is particularly advantageous in scenarios where object lifetimes are managed dynamically.
  3. Non-enumerable: Objects within a WeakSet are not enumerable, meaning it doesn't provide methods to retrieve specified objects explicitly.

JavaScript WeakSet Methods

Let's explore the core methods provided by the WeakSet object along with their descriptions.

  1. add(object): Adds a new object to the end of the WeakSet object.
  2. delete(object): Removes the specified object from the WeakSet object.
  3. has(object): Indicates whether the WeakSet object contains the specified object element.

TypeScript and ES7

Now, let's adapt the JavaScript WeakSet object to TypeScript, embracing the benefits of static typing.

class CustomWeakSet<T extends object> {
    private weakSet: WeakSet<T>;

    constructor(iterable?: Iterable<T>) {
        this.weakSet = new WeakSet(iterable);
    }

    /**
     * Adds a new object to the WeakSet.
     * @param object The object to add to the WeakSet.
     */
    add(object: T): void {
        this.weakSet.add(object);
    }

    /**
     * Removes the specified object from the WeakSet.
     * @param object The object to remove from the WeakSet.
     * @returns True if the object was present and removed, false otherwise.
     */
    delete(object: T): boolean {
        return this.weakSet.delete(object);
    }

    /**
     * Checks whether the WeakSet contains the specified object.
     * @param object The object to check for in the WeakSet.
     * @returns True if the object is present, false otherwise.
     */
    has(object: T): boolean {
        return this.weakSet.has(object);
    }
}

// Example Usage
const myTypedWeakSet = new CustomWeakSet<object>([{ prop: 'value' }, { prop: 'anotherValue' }]);
const newObject = { prop: 'yetAnotherValue' };
myTypedWeakSet.add(newObject);
console.log(myTypedWeakSet.has(newObject)); // Output: true

In this TypeScript adaptation, we define a CustomWeakSet class that encapsulates the WeakSet object. The class enforces a constraint (T extends object) to ensure that only objects can be added to the WeakSet, enhancing type safety.

The WeakSet object emerges as a specialized entity, designed explicitly for managing weakly held object references. As we explored the syntax, characteristics, and methods of the JavaScript WeakSet, it became evident that this data structure offers a unique set of advantages, particularly in scenarios where object lifetimes need dynamic management.

Unlike its counterpart, the Set object, the WeakSet exclusively deals with objects, ensuring uniqueness within its realm. Its distinctive feature of enabling automatic garbage collection for unreferenced objects positions it as a powerful tool for scenarios where memory efficiency is a priority. Moreover, the non-enumerable nature of objects within a WeakSet signifies a deliberate design choice, highlighting its role as a behind-the-scenes manager of object references.

Translating these concepts into the realm of TypeScript, we crafted a CustomWeakSet class that not only encapsulates the functionality of the WeakSet but also harnesses TypeScript's type system to provide enhanced type safety. This adaptation seamlessly integrates the benefits of static typing while preserving the unique characteristics of the WeakSet. The JavaScript WeakSet, when wielded in ES7 and TypeScript, becomes a valuable asset for developers navigating scenarios where weakly held object references and efficient memory management are essential. As we continue to explore the capabilities of JavaScript in evolving landscapes, the WeakSet stands out as a testament to the adaptability and sophistication of the language.


Similar Articles