Created
October 18, 2022 09:14
-
-
Save semlinker/85ea41696f9b098a762f889b8781aff1 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Trace Property Access
This file contains hidden or 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 tracePropertyAccess(obj, propKeys) { | |
| const propKeySet = new Set(propKeys); | |
| return new Proxy(obj, { | |
| get(target, propKey, receiver) { | |
| if (propKeySet.has(propKey)) { | |
| console.log("GET " + propKey); | |
| } | |
| return Reflect.get(target, propKey, receiver); | |
| }, | |
| set(target, propKey, value, receiver) { | |
| if (propKeySet.has(propKey)) { | |
| console.log("SET " + propKey + "=" + value); | |
| } | |
| return Reflect.set(target, propKey, value, receiver); | |
| }, | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment