Skip to content

Instantly share code, notes, and snippets.

@eduardoromero
Created January 17, 2019 05:24
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 eduardoromero/49cdb7c5755ba3c244e3c2db606c4794 to your computer and use it in GitHub Desktop.
Save eduardoromero/49cdb7c5755ba3c244e3c2db606c4794 to your computer and use it in GitHub Desktop.
observe-proxy
const Observe = (AnObject) => {
return new Proxy(AnObject, {
get (target, prop) {
const method = target[prop]
if (typeof method === 'function') {
console.log(`\`${method}\` was accessed`)
} else {
console.log(`\`${prop}\` was accessed, and has value of ${target[prop]}`)
}
return target[prop]
},
set (obj, prop, value) {
const oldValue = obj[prop]
obj[prop] = value;
console.log(`${prop} changed from \`${oldValue}\` to \`${value}\`.`)
return true
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment