33 lines
799 B
Markdown
33 lines
799 B
Markdown
---
|
|
title: Deep freeze object
|
|
tags: object,recursion
|
|
expertise: intermediate
|
|
cover: blog_images/frozen-globe.jpg
|
|
firstSeen: 2018-08-25T18:54:16+03:00
|
|
lastUpdated: 2020-10-19T18:51:03+03:00
|
|
---
|
|
|
|
Deep freezes an object.
|
|
|
|
- Use `Object.keys()` to get all the properties of the passed object, `Array.prototype.forEach()` to iterate over them.
|
|
- Call `Object.freeze()` recursively on all properties, applying `deepFreeze()` as necessary.
|
|
- Finally, use `Object.freeze()` to freeze the given object.
|
|
|
|
```js
|
|
const deepFreeze = obj => {
|
|
Object.keys(obj).forEach(prop => {
|
|
if (typeof obj[prop] === 'object') deepFreeze(obj[prop]);
|
|
});
|
|
return Object.freeze(obj);
|
|
};
|
|
```
|
|
|
|
```js
|
|
'use strict';
|
|
|
|
const val = deepFreeze([1, [2, 3]]);
|
|
|
|
val[0] = 3; // not allowed
|
|
val[1][0] = 4; // not allowed as well
|
|
```
|