4 ms·
That would be a neat interface too, but it would necessitate a somewhat different approach than the one taken in the article. You can only use a proxy to trap o
by foob 8y ago
That would be a neat interface too, but it would necessitate a somewhat different approach than the one taken in the article. You can only use a proxy to trap operations that are already supported by the object that's being wrapped. The SliceArray class inherits from the builtin Array, and it therefore doesn't support the call operation. You would need to initialize a second custom class that inherits from Function, wrap that with a proxy, and then use the SliceArray as the actual target inside of the traps. It's definitely doable, but it's more complicated than one might expect.
I mentioned in the article that Remote Browser is an interesting project to look at to see some more advanced proxy usage. One of the things that it does is to define a CallableProxy class [1] which facilitates attaching a call handler to objects that otherwise wouldn't support them. The code itself isn't complicated at all, but it takes a bit of thought to make the pieces fit together just right.
- [1] - https://github.com/intoli/remote-browser/blob/9ffdd1b5b0a9b43426d793e529cf7d9596f986ec/src/browser.js#L11 https://github.com/intoli/remote-browser/blob/9ffdd1b5b0a9b4...