Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Created May 2, 2023 06:22
Show Gist options
  • Save codeAdrian/08c573ed8aac8257c483807eeeed7066 to your computer and use it in GitHub Desktop.
Save codeAdrian/08c573ed8aac8257c483807eeeed7066 to your computer and use it in GitHub Desktop.
const user = {
firstName: "John",
};
const getHandler = (obj, prop) => {
// get object attributes
if(prop in obj) {
return Reflect.get(obj, prop); // Use object's static method
}
// Extension - Custom attribute
if(prop === "nickname") {
return "Johnny";
}
// Handle error
return undefined;
}
const userProxy = new Proxy(user, {
get: getHandler,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment