Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created March 1, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkWiiPlayer/e10420fb3ac74929c783ad164b757185 to your computer and use it in GitHub Desktop.
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
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