Created
February 13, 2023 18:24
-
-
Save metruzanca/0513249bd01babc780a913506dbfd5d0 to your computer and use it in GitHub Desktop.
Using Proxy API to detect a change in an object and calling a function after the update.
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
function onChange(data, callback) { | |
return new Proxy(data, { | |
set(target, prop, value) { | |
target[prop] = value; | |
callback(target, value); | |
return true; | |
} | |
}); | |
} | |
const target = { | |
message1: "hello", | |
message2: "everyone", | |
}; | |
const proxy = onChange(target, (target, value) => { | |
console.log(target, value); | |
}) | |
proxy.message1 = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment