Created
December 22, 2017 11:53
-
-
Save abinavseelan/02edc2388d4dc3843c86d19f40434733 to your computer and use it in GitHub Desktop.
Proxy Object - Private Properties
This file contains hidden or 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 handler = { | |
| get: function(obj, prop) { | |
| if (prop === 'id') { // Check if the id is being accessed | |
| throw new Error('Cannot access private properties!'); // Throw an error | |
| } else { | |
| return obj[prop]; // If it's not the id property, return it as usual | |
| } | |
| } | |
| } | |
| const person = { | |
| id: 1, | |
| name: 'Foo Bar' | |
| } | |
| const proxiedPerson = new Proxy(person, handler); | |
| console.log(proxiedPerson.id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment