Skip to content

Instantly share code, notes, and snippets.

@codeAdrian
Created April 28, 2023 13:02
Show Gist options
  • Save codeAdrian/39a69109c57baccd1154eccb14505293 to your computer and use it in GitHub Desktop.
Save codeAdrian/39a69109c57baccd1154eccb14505293 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 obj[prop];
}
// Extension - Custom attribute
if(prop === "nickname") {
return "Johnny";
}
// Handle error
return undefined;
}
const userProxy = new Proxy(user, {
get: getHandler,
});
console.log(userProxy.firstName); // "John"
console.log(userProxy.nickname); // "Johnny"
console.log(userProxy.age); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment