Javascript WeakSet constructor

Introduction

The WeakSet constructor creates WeakSet objects.

new WeakSet([iterable]);
Parameters Meaning
iterablean iterable object

If an iterable object is passed, all of its elements will be added to the new WeakSet.

null is treated as undefined in WeakSet.

var ws = new WeakSet();
var foo = {};//from  w w  w.  j a v  a  2s  . c om
var bar = window;

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

let a = ws.has(foo);    // true
console.log(a);
ws.has(bar);    // true
console.log(a);

a = ws.delete(foo); // removes foo from the set
console.log(a);

a = ws.has(foo);    // false, foo has been removed
console.log(a);

a = ws.has(bar);    // true, bar is retained
console.log(a);



PreviousNext

Related