add deepFreeze

This commit is contained in:
Stefan Feješ
2018-08-25 17:54:16 +02:00
parent a86bd7eff9
commit f1fb6aadb5
6 changed files with 3099 additions and 3033 deletions

38
snippets/deepFreeze.md Normal file
View File

@ -0,0 +1,38 @@
### deepFreeze
Deep freezes an object.
Calls `Object.freeze(obj)` recursively on all unfrozen properties of obj that are `typeof` function or object.
```js
const deepFreeze = obj => {
Object.freeze(obj);
Object.getOwnPropertyNames(obj).forEach(function(prop) {
if (
obj.hasOwnProperty(prop) &&
obj[prop] !== null &&
(typeof obj[prop] === 'object' || typeof obj[prop] === 'function') &&
!Object.isFrozen(obj[prop])
) {
deepFreeze(obj[prop]);
}
});
return obj;
};
```
```js
'use strict';
const a = Object.freeze([1, [2, 3]]);
a[0] = 3; // not allowed
a[1][0] = 4; // allowed but shouldn't be
const b = deepFreeze([1, [2, 3]]);
b[0] = 3; // not allowed
b[1][0] = 4; // not allowed as well
```