Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Created December 22, 2017 11:53
Show Gist options
  • Select an option

  • Save abinavseelan/02edc2388d4dc3843c86d19f40434733 to your computer and use it in GitHub Desktop.

Select an option

Save abinavseelan/02edc2388d4dc3843c86d19f40434733 to your computer and use it in GitHub Desktop.
Proxy Object - Private Properties
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