Files
30-seconds-of-code/snippets/frozenSet.md
Isabelle Viktoria Maciohsek a406ddcb34 Add frozenSet
2020-10-11 11:52:48 +03:00

584 B

title, tags
title tags
frozenSet array,intermediate

Creates a frozen Set object.

  • Use the new Set() constructor to create a new Set object from iterable.
  • Set the add, delete and clear methods of the newly created object to undefined, so that they cannot be used, practically freezing the object.
const frozenSet = iterable => {
  const s = new Set(iterable);
  s.add = undefined;
  s.delete = undefined;
  s.clear = undefined;
  return s;
};
frozenSet([1, 2, 3, 1, 2]); 
// Set { 1, 2, 3, add: undefined, delete: undefined, clear: undefined }