Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 18, 2022 06:35
Show Gist options
  • Save semlinker/6ba3dccf027060a10c7b6a45b7b017c9 to your computer and use it in GitHub Desktop.
Save semlinker/6ba3dccf027060a10c7b6a45b7b017c9 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Enhanced Object
const enhancedObject = (target) =>
new Proxy(target, {
get(target, property) {
if (property in target) {
return target[property];
} else {
return getPropertyValue(property, target);
}
},
});
let value;
function getPropertyValue(property, target) {
value = null;
for (const key of Object.keys(target)) {
if (typeof target[key] === "object") {
getPropertyValue(property, target[key]);
} else if (typeof target[property] !== "undefined") {
value = target[property];
break;
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment