Created
November 15, 2017 12:59
-
-
Save CraigChilds94/57a8e94168d71a786ed8c8b64df774c1 to your computer and use it in GitHub Desktop.
Object Proxy-ing
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
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made this so I remember that it exists lol