Created
October 18, 2022 06:35
-
-
Save semlinker/6ba3dccf027060a10c7b6a45b7b017c9 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Enhanced Object
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 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