Last active
February 5, 2018 18:12
-
-
Save aholachek/138bb8a0189c51d2285cbf5d8afdc110 to your computer and use it in GitHub Desktop.
Simple JS 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
// a handler object specifies which methods to proxy | |
// this one will simply override JavaScript’s default object get method | |
const loggingHandler = { | |
get(target, property) { | |
const val = target[property] | |
console.log(`Property "${property}" returned "${JSON.stringify(val)}"!`) | |
return val | |
} | |
} | |
const data = { foo: 'bar' } | |
const loggedObject = new Proxy(data, loggingHandler) | |
// now, let's access a property to test that it works | |
const foo = loggedObject.foo | |
// Logged to the console: Property "foo" returns "bar" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment