have people used Javascript’s new “Proxy” tool?

Wow, I guess I really haven’t been paying attention (for years) to new Javascript features. Have people seen this Proxy thing? It’s wild!

Apparently it is now possible to (among other things) properly fake an Array object, including the square bracket index syntax. Something which was always out of reach before. The mechanism feels kind of hacky, but that’s probably necessary for backwards compatibility reasons.

This means we can make a “real” Python-style range object which generally pretends to be an array and supports all of the relevant parts of the Array API, but can be used in a loop / as an iterable just computing one term at a time (like the Observable standard library’s Generators.range, does fast membership testing (.includes), and doesn’t take any memory.

I’m not up with what people are doing in JS land; maybe these new features are common knowledge. Has anyone seen uses (either clever or prosaic) of Proxy in production anywhere?

1 Like

Wow, that’s super cool! I think Proxy hasn’t seen full use yet since it’s super hard to polyfill and it still isn’t supported in IE. It’s definitely something that could see a lot more use though.

I wonder what proportion of Observable notebooks work in IE. This is probably one of the places where 4–5 year-old browser features can be most safely used without additional compatibility concerns.

Anyway, I expect Proxy could be used for implementing quite a few interesting data structures and making them work more or less transparently like the built-ins.

Haha, yeah, proxies are crazy :slight_smile:

I know them from libraries like MobX (I think they use proxies), and these “immutability with less pain” kind of libraries like immer. Any my beloved Vue, where state management is handled similarly to how Mobx does it, I believe they’re transitioning (or planning to transition) from automatically adding get/setters (as a way of enabling reactivity) to using proxies.

But I don’t think I know of any clever uses in fun libraries outside of this “reactivity” realm of things.

4 Likes

Wow, immer seems great!

Some more links here, https://github.com/mikaelbr/awesome-es2015-proxy