Last active
April 20, 2018 18:28
-
-
Save donabrams/d91587c77dfb83f74270fac991f2cb76 to your computer and use it in GitHub Desktop.
Deep Read Only
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
const a = { | |
foo: "bar", | |
beep: { | |
boop: 1, | |
}, | |
baz: [ | |
true, | |
] | |
}; | |
const deepReadOnly = (a) => new Proxy(a, { | |
get: function(target, prop, receiver) { | |
const a = Reflect.get(...arguments); | |
return typeof a === "object" ? deepReadOnly(a) : a; | |
}, | |
set: () => {} | |
}); | |
const b = deepReadOnly(a); | |
console.log(b.beep.boop); | |
b.beep.boop = 3; | |
console.log(a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment