Javascript WeakMap constructor

Introduction

The WeakMap() constructor creates WeakMap objects.

new WeakMap([iterable])
Parameter Meaning
iterable an Array or other iterable object.

Using WeakMap

const wm1 = new WeakMap(),/*from  www .  ja  va 2  s. c o m*/
      wm2 = new WeakMap(),
      wm3 = new WeakMap();
const o1 = {},
      o2 = function() {},
      o3 = function() {};

wm1.set(o1, 37);
wm1.set(o2, 'ASDF');
wm2.set(o1, o2);
wm2.set(o3, undefined);
wm2.set(wm1, wm2);

console.log(wm1);
console.log(wm2);

let a = wm1.get(o2);
a = wm2.get(o2);
console.log(a);
a = wm2.get(o3);
console.log(a);

let b = wm1.has(o2); 
console.log(b);
b = wm2.has(o2); 
console.log(b);
b = wm2.has(o3);
console.log(b);

wm3.set(o1, 37);
a = wm3.get(o1);
console.log(a);

b = wm1.has(o1);
console.log(b);

wm1.delete(o1);
b = wm1.has(o1);
console.log(b);



PreviousNext

Related