Add frozenSet

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-11 11:52:48 +03:00
parent f1e7e8c9d9
commit a406ddcb34

24
snippets/frozenSet.md Normal file
View File

@ -0,0 +1,24 @@
---
title: frozenSet
tags: 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.
```js
const frozenSet = iterable => {
const s = new Set(iterable);
s.add = undefined;
s.delete = undefined;
s.clear = undefined;
return s;
};
```
```js
frozenSet([1, 2, 3, 1, 2]);
// Set { 1, 2, 3, add: undefined, delete: undefined, clear: undefined }
```