Skip to content

Instantly share code, notes, and snippets.

@aholachek
Last active February 5, 2018 18:12
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 aholachek/138bb8a0189c51d2285cbf5d8afdc110 to your computer and use it in GitHub Desktop.
Save aholachek/138bb8a0189c51d2285cbf5d8afdc110 to your computer and use it in GitHub Desktop.
Simple JS Proxy
// 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