Javascript WeakMap set()

Introduction

The Javascript WeakMap set() method adds a new key-value pair to a WeakMap object.

This function returns the WeakMap object.

wm.set(key, value);
Parameters Optional Meaning
keyRequired Must be object.
value Required. Any value.

Using the set method

let window = function() {};

var wm = new WeakMap();
var obj = {};/*from   w ww  .j a v a2  s .c om*/

// Add new elements to the WeakMap
wm.set(obj, 'foo').set(window, 'bar'); // chainable
console.log(wm);

// Update an element in the WeakMap
wm.set(obj, 'AAA');
console.log(wm);



PreviousNext

Related