Skip to content

Instantly share code, notes, and snippets.

@amysimmons
Created July 18, 2018 07:19
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 amysimmons/4d0b94b2df4b651d176b6f10f1d30add to your computer and use it in GitHub Desktop.
Save amysimmons/4d0b94b2df4b651d176b6f10f1d30add to your computer and use it in GitHub Desktop.
const obj = { a: 1 };
const handlers = {
  // declare a get method on the handler object
  get(target, key, context) {
    console.log( "accessing: ", key );
    // forward the operation onto the target (obj) via Reflect.get
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get
    // target is the object, key is the key being accessed, context is the proxy 
    return Reflect.get(
      target, key, context
    );
  }
};
const pobj = new Proxy( obj, handlers );

obj.a; // 1

pobj.a; 
// accessing: a
// 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment