Created
March 7, 2020 12:49
-
-
Save kakaroto/c544479caea31ed9a3a0874bf1d7fa64 to your computer and use it in GitHub Desktop.
Proxy to look at what get/set/call happens in an object
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
| const rand = Math.round(Math.random() * 1000); | |
| const proxy = new Proxy(toProxy, { | |
| get: (obj, prop) => { | |
| const value = obj[prop]; | |
| console.log("GET : ", rand, prop, value); | |
| if (value instanceof Function) { | |
| return new Proxy(value, { | |
| apply: (func, thisArg, argumentList) => { | |
| console.log("APPLY : ", rand, prop, argumentList); | |
| const ret = func.apply(thisArg, argumentList); | |
| console.log("RETURN : ", rand, prop, ret); | |
| return ret; | |
| } | |
| }); | |
| } | |
| return value; | |
| }, | |
| set: (obj, prop, value) => { | |
| console.log("SET : ", rand, prop, value); | |
| obj[prop] = value; | |
| return true; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment