Last active
October 18, 2022 09:34
-
-
Save semlinker/718ecfc6234cd2b6664afb204e0fa6cd to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Hide Property
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
function hideProperty(target, prefix = "_") { | |
return new Proxy(target, { | |
has: (obj, prop) => !prop.startsWith(prefix) && prop in obj, | |
ownKeys: (obj) => | |
Reflect.ownKeys(obj).filter( | |
(prop) => typeof prop !== "string" || !prop.startsWith(prefix) | |
), | |
get: (obj, prop, rec) => (prop in rec ? obj[prop] : undefined), | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment