Javascript WeakSet Type

Introduction

The WeakSet stores weakly held objects.

Unlike Sets, WeakSet only stores objects. We cannot store any values of any type in WeakSet.

References to objects in Weakset are held weakly.

If there is no other reference to an object stored in the WeakSet, they can be garbage collected.

const ws = new WeakSet();//from ww w.j  a v  a2  s.  c  om
const foo = {};
const bar = function(){};

ws.add(foo);
console.log(ws);
ws.add(bar);
console.log(ws);

a = ws.has(foo);
console.log(a);

a = ws.has(bar);
console.log(a);

a = ws.delete(foo);
console.log(a);
a = ws.has(foo);
console.log(a);
a = ws.has(bar);
console.log(a);



PreviousNext

Related