Skip to content

Instantly share code, notes, and snippets.

@CraigChilds94
Created November 15, 2017 12:59
Show Gist options
  • Save CraigChilds94/57a8e94168d71a786ed8c8b64df774c1 to your computer and use it in GitHub Desktop.
Save CraigChilds94/57a8e94168d71a786ed8c8b64df774c1 to your computer and use it in GitHub Desktop.
Object Proxy-ing
var myObject = {
testing: 'Hello',
};
var myProxiedObject = new Proxy(myObject, {
get: function(target, prop) {
console.log({ type: 'get', target, prop });
return Reflect.get(target, prop);
},
set: function(target, prop, value) {
console.log({ type: 'set', target, prop, value });
return Reflect.set(target, prop, value);
}
});
myProxiedObject.testing = 'World';
// Results in logging of target and value changed. Type "set"
console.log(myProxiedObject.testing);
// Results in logging of target and prop. Type "get"
// Could be combined with an event system and/or DOM manipulation to update UI when object is changed.
// see https://github.com/GoogleChrome/proxy-polyfill for IE 9+ support.
@CraigChilds94
Copy link
Author

Made this so I remember that it exists lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment