Created
December 23, 2021 09:06
-
-
Save hitautodestruct/ed2be9a9f4214fef1fb024c7ba650ac3 to your computer and use it in GitHub Desktop.
A method for recursively freezing nested objects, for use with vuex store
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
function deepFreeze (object) { | |
if (object) { | |
// Retrieve the property names defined on object | |
var propNames = Object.getOwnPropertyNames(object) | |
// Freeze properties before freezing self | |
for (let name of propNames) { | |
let value = object[name] | |
object[name] = value && typeof value === 'object' ? DataMigration.deepFreeze(value) : value | |
} | |
object = Object.freeze(object) | |
} | |
return object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment