Created
January 17, 2019 05:24
-
-
Save eduardoromero/49cdb7c5755ba3c244e3c2db606c4794 to your computer and use it in GitHub Desktop.
observe-proxy
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 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