JavaScript WeakMap Object

The JavaScript WeakMap object is a type of collection which is almost similar to Map. It stores each element as a key-value pair where keys are weakly referenced. Here, the keys are objects and the values are arbitrary values.

Syntax

new WeakMap([iterable])  

Parameter

iterable – It represents an array and other iterable object whose elements are in the form of key-value pair.

Points to remember

  • A WeakMap object allows the keys of object type only.
  • In WeakMap, if there is no reference to a key object, they are targeted to garbage collection.
  • In WeakMap, the keys are not enumerable. So, it doesn’t provide any method to get the list of keys.
  • A WeakMap object iterates its elements in insertion order.

JavaScript WeakMap Methods

MethodsDescription
delete()It deletes the specified element from the WeakMap object.
get()It returns the value of specified key.
has()It indicates whether the WeakMap object contains the specified value element.
set()It adds or updates the key-value pairs to WeakMap object.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *