ES6 in Depth
  • Introduction
  • Installation
  • Block Scope
    • Constant
    • Use cases
  • Template String
    • New String Methods
    • Tagged Template Literal
  • New Number Methods
  • Arrow Function
    • Default Parameters
    • Lexical This Scope
    • Where not to use arrow function
  • Object Enhancement
  • New Array Methods
    • For Of
  • Spread Operator
  • Destructuring
    • Array Destructuring
  • Class
  • Symbols
  • New Data Structures
    • Set
    • Map
    • WeakSet
    • WeakMap
    • Iterators
    • Generators
  • Promise
  • Import Export
Powered by GitBook
On this page
  • No clear() method on weakset
  • WeakSet Demo

Was this helpful?

  1. New Data Structures

WeakSet

  • References to keys/values held weakly

  • Do not prevent garbage collection

  • Small api ( add, delete, check for values ) compared to set/map

  • cann't be iterated

Note

  • keys must be objects

  • the values can be arbitrary values.

var ws = new WeakSet();
ws.add()
ws.delete()
ws.has()

No clear() method on weakset

As if their is no reference to object wekset runs garbage collection so we have no need to worry about it

let dog1 = {name: 'tomy'};
let dog2 = {name: 'fluffy'};

const ws = new WeakSet([dog1, dog2]);
console.log(ws); // WeakSet {Object {name: "fluffy"}, Object {name: "tomy"}}

dog1 =null;

console.log(ws); // WeakSet {Object {name: "fluffy"}}, after some tome

WeakSet Demo

var ws = new WeakSet();
var age = {age: 20};
ws.add({name: 'hemant'}).add(age);
console.log(ws.delete(age)); // true
console.log(ws.has(age)); // false
console.log(ws); // WeakSet {Object {name: "hemant"}}
PreviousMapNextWeakMap

Last updated 4 years ago

Was this helpful?