Set
Initialization
var set = new Set(['a', 'b', 'c', 'c']);

console.log(set); //a, b, c

var set2 = new Set('abcc');

console.log(set2); //a, b, c

var set3 = new Set([1, 2, 3, 3]);

console.log(set3); //1, 2, 3
		
Methods
var set = new Set();

//add
set.add(1);
set.add(2);
set.add(3);
set.add({name: 'Lin'});

console.log(set);

//entries
var itr = set.entries(); //iterator object
for (const [v, k] of itr)
	console.log(v, k);

//forEach
set.forEach((value) => console.log(value));

//has
console.log(set.has(2));

//values
var itr2 = set.values(); // iterator object
for (const v of itr2)
	console.log(v);
		
var set = new Set(['a', 'b', 'c', 'c']);

set.delete('a');
console.log(set); //{'b', 'c'}

set.clear();
console.log(set); //{}
		
WeakSet
  • Weak sets can only contain objects
  • the objects they contain may be garbage collected
  • weak sets cannot be iterated
  • let weakSet = new WeakSet();
    
    let obj = {};
    let obj2 = {name: 'Lin'};
    
    weakSet.add(obj);
    weakSet.add(obj2);
    
    console.log(weakSet.has(obj));