Skip to content

Commit

Permalink
Questionfy free-set-object
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Feb 16, 2024
1 parent d0845dc commit e397631
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
9 changes: 6 additions & 3 deletions content/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,6 @@
- from: /js/s/pad
to: /js/s/pad-string
status: 301!
- from: /js/s/frozen-set
to: /js/s/freeze-set-object
status: 301!
- from: /js/s/prefix
to: /js/s/prefix-css-property
status: 301!
Expand Down Expand Up @@ -2608,3 +2605,9 @@
- from: /js/s/function-based-array-element-equality
to: /js/s/all-values-of-array-are-equal
status: 301!
- from: /js/s/frozen-set
to: /js/s/freeze-set-or-map
status: 301!
- from: /js/s/freeze-set-object
to: /js/s/freeze-set-or-map
status: 301!
26 changes: 0 additions & 26 deletions content/snippets/js/s/freeze-set-object.md

This file was deleted.

54 changes: 54 additions & 0 deletions content/snippets/js/s/freeze-set-or-map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Can I freeze a Set or Map in JavaScript?
shortTitle: Freeze Set or Map
type: question
language: javascript
tags: [array]
cover: reflection-on-lake
excerpt: Learn how to create a frozen `Set` or `Map` in JavaScript.
dateModified: 2024-02-15
---

Freezing an object is rather simple, using `Object.freeze()`. However, when it comes to freezing a `Set` or `Map`, things get a bit more complicated. The `Set` and `Map` objects have their own methods for adding, deleting and clearing elements, which can be problematic when trying to freeze them.

Luckily, as they're objects after all, you can easily **override these methods** to prevent them from being used.

## Freeze a `Set` object

In order to freeze a `Set` object, you can simply set the `Set.prototype.add()`, `Set.prototype.delete()` and `Set.prototype.clear()` methods to `undefined`. This will effectively prevent them from being used, practically freezing the object. In addition to that, you can use `Object.freeze()` to freeze the `Set` object itself.

```js
const freezeSet = iterable => {
const s = new Set(iterable);
s.add = undefined;
s.delete = undefined;
s.clear = undefined;
return Object.freeze(s);
};

freezeSet([1, 2, 3, 1, 2]);
/* Set {
1, 2, 3,
add: undefined, delete: undefined, clear: undefined
} */
```

## Freeze a `Map` object

Freezing a `Map` object is very similar to freezing a `Set` object. You can set the `Map.prototype.set()`, `Map.prototype.delete()` and `Map.prototype.clear()` methods to `undefined` and then use `Object.freeze()` to freeze the `Map` object itself.

```js
const freezeMap = iterable => {
const m = new Map(iterable);
m.set = undefined;
m.delete = undefined;
m.clear = undefined;
return Object.freeze(m);
};

freezeMap([['a', 1], ['b', 2]]);
/* Map {
'a' => 1, 'b' => 2,
set: undefined, delete: undefined, clear: undefined
} */
```

0 comments on commit e397631

Please sign in to comment.