Update deepFreeze.md
fix typo in line 15, and add missing typeof call
This commit is contained in:
@ -6,13 +6,13 @@ tags: object,recursion,intermediate
|
|||||||
Deep freezes an object.
|
Deep freezes an object.
|
||||||
|
|
||||||
Use `Object.keys()` to get all the properties of the passed object, `Array.prototype.forEach()` to iterate over them.
|
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.
|
Finally, use `Object.freeze()` to freeze the given object.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const deepFreeze = obj => {
|
const deepFreeze = obj => {
|
||||||
Object.keys(obj).forEach(prop => {
|
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);
|
return Object.freeze(obj);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user