hotfix: unfrozenObject in frozenObject return wrong value (#1207)

Update deepFreeze.md

Co-authored-by: Angelos Chalaris <chalarangelo@gmail.com>
This commit is contained in:
銀桑
2020-09-05 00:50:53 +08:00
committed by GitHub
parent de3832092d
commit 90b8f077fa

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, 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' && !Object.isFrozen(obj[prop])) deepFreeze(obj[prop]);
if (typeof(obj[prop]) === 'object') deepFreeze(obj[prop]);
});
return Object.freeze(obj);
};