/  Technology   /  JavaScript WeakMap Object

JavaScript WeakMap Object

 

A JavaScript WeakMap object is a type of collection that is similar to a Map. Each element is stored as a key-value pair with a weakly referenced key. In this case, the keys are objects and the values are arbitrary.

Syntax

new WeakMap([iterable]) 

Parameter

iterable – Arrays and other iterable objects whose elements are key-value pairs are represented by this type.

Points to remember

  • WeakMap objects support only keys of object types.
  • In WeakMap, if there is no reference to a key object, the object is targeted for garbage collection.
  • WeakMap does not enumerate its keys. As a result, it does not provide any method for retrieving the list of keys.
  • WeakMap objects iterate their elements in insertion order.

JavaScript WeakMap Methods

MethodsDescription
delete()The function deletes the specified element from the WeakMap object.
get()Upon specifying a key, it returns its value.
has()The value element indicates whether the WeakMap object contains the specified value.
set()This method adds or updates key-value pairs to the WeakMap object.

 

Leave a comment