Created
April 28, 2023 13:02
-
-
Save codeAdrian/39a69109c57baccd1154eccb14505293 to your computer and use it in GitHub Desktop.
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 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