update deepFreeze

This commit is contained in:
Stefan Feješ
2018-09-01 12:01:02 +02:00
parent 87552862c3
commit 21632f87b6
2 changed files with 1567 additions and 1581 deletions

3112
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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