Skip to content

Instantly share code, notes, and snippets.

@fritzy
Last active December 30, 2015 22:53
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 fritzy/3ef4ab6f64057963ea12 to your computer and use it in GitHub Desktop.
Save fritzy/3ef4ab6f64057963ea12 to your computer and use it in GitHub Desktop.
Basic Proxy Example
const someObject = {};
const someProxy = new Proxy(someObject, {
get: function (target, property, reciever) {
if (property.substr(0, 6) === 'happy_') {
return Reflect.get(target, property.substr(6), reciever);
}
return 'sad';
},
set: function (target, property, value, reciever) {
if (property.substr(0, 6) === 'happy_') {
return Reflect.set(target, property.substr(6), value, reciever);
}
return false;
}
});
someProxy.happy_x = 'happy';
console.log(`someproxy.happy_x = 'happy';`);
console.log('someProxy.x =', someProxy.x);
console.log('someObject.x =', someObject.x);
console.log('someProxy.happy_x =', someProxy.happy_x);
console.log('someObject.happy_x =', someObject.happy_x);
someproxy.happy_x = 'happy';
someProxy.x = sad
someObject.x = happy
someProxy.happy_x = happy
someObject.happy_x = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment