Created
December 1, 2016 13:20
-
-
Save newmanbrad/bf83d49bfaa0bfb4094fe9f2b0548bef to your computer and use it in GitHub Desktop.
Deep Freeze Object Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Object.freeze() will not freeze nested objects. The function below | |
// will freeze all nested objects for immutability. | |
let isObject = (val) => val && typeof val === 'object'; | |
function deepFreezeObject(obj) { | |
if(isObject(obj) && !Object.isFrozen(obj)){ | |
// Recusively call until all child objects are frozen | |
Object.keys(obj).forEach(name => deepFreezeObject(obj[name])); | |
Object.freeze(obj); | |
} | |
return obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment