Update deepFreeze.md

fix typo in line 15, and add missing typeof call
This commit is contained in:
Roni Bar-David
2020-07-20 09:56:30 +03:00
committed by GitHub
parent 739ddd4e36
commit 77e605b044

View File

@ -6,13 +6,13 @@ tags: object,recursion,intermediate
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(obj)` recursively on all properties, checking if each one is frozen using `Object.isFrozen()` and applying `deepFreeze()` as necessary.
Call `Object.freeze(obj)` recursively on all properties, checking if each object is frozen using `Object.isFrozen()` and applying `deepFreeze()` as necessary.
Finally, use `Object.freeze()` to freeze the given object.
```js
const deepFreeze = obj => {
Object.keys(obj).forEach(prop => {
if (obj[prop] === 'object' && !Object.isFrozen(obj[prop])) deepFreeze(v[prop]);
if (typeof(obj[prop]) === 'object' && !Object.isFrozen(obj[prop])) deepFreeze(obj[prop]);
});
return Object.freeze(obj);
};