Created
March 1, 2022 12:01
-
-
Save DarkWiiPlayer/e10420fb3ac74929c783ad164b757185 to your computer and use it in GitHub Desktop.
Proof of concept of hijacking individual keys directly in an object and assigning them change callbacks
This file contains hidden or 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
values = Symbol("values") | |
hijack = (object, callbacks) => { | |
const val = object[values] ||= Object.create(null) | |
Object.entries(callbacks).forEach(([key, callback]) => { | |
Object.defineProperty(object, key, { | |
get() { return val[key] }, | |
set(value) { | |
callback(value) | |
val[key] = value | |
return true | |
} | |
}) | |
}) | |
return object | |
} | |
o = hijack({}, { | |
foo: console.log, | |
bar: value => console.log(`Bar: ${value}`) | |
}) | |
o.foo = 20 | |
o.bar = 30 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment