3 ms·
Proxy? How does it work?
by newhotelowner 4y ago
Proxy? How does it work?
- Waterluvian 4y agohttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Imagine an Object where you control _all_ the getting and setting.
- wruza 4y agoAnd it is also slow as hell, so yeah, don’t abuse it for regular programming or in hot loops.
- no_way 4y agoIt was very slow before, but browsers improved performance a lot, multiple frameworks use proxies for their reactive primitives under the hood, Vue, Mobx and so on. SolidJS is one of the fastests frameworks out there and is built mainly on proxies, so proxies themselves can't be that slow. Granted in very very tight loop regular object get/set will be faster, but performance is more than good enough today. If you have an use case for it, use it.
- Waterluvian 4y agoImmer relies completely on it and is wicked fast. It did improve by a lot in recent times.
- svieira 4y agoIt lets you intercept behaviors as they are requested of your object and do whatever you want to respond to them. Think of them as extensions of descriptors and you won't be far off (except they're designed to be a wrapper instead of methods on an existing object so that they can also be used for security, IIRC) See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- dezmou 4y agoProxy is the thing that make the framework Angular possible, it can for example watch an object and when a property change, update the front end