Skip to content

Instantly share code, notes, and snippets.

@newmanbrad
Created December 1, 2016 13:20
Show Gist options
  • Save newmanbrad/bf83d49bfaa0bfb4094fe9f2b0548bef to your computer and use it in GitHub Desktop.
Save newmanbrad/bf83d49bfaa0bfb4094fe9f2b0548bef to your computer and use it in GitHub Desktop.
Deep Freeze Object Javascript
// 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