Last active
December 30, 2015 22:53
-
-
Save fritzy/3ef4ab6f64057963ea12 to your computer and use it in GitHub Desktop.
Basic Proxy Example
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
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); |
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
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