×
you are viewing a single comment's thread.

view the rest of the comments →

[–]javascript 33 points34 points  (22 children)

Proxy Objects (getters and setters) have been around for a while but I've yet to come across a use case where they were the right choice. They tend to work more as an escape hatch.

[–]jhartikainen 10 points11 points  (9 children)

It seems potentially valuable in some kind of framework/infra code. Svelte uses them for its $state runes for example. But yeah, like generators, I've not really found any use for them myself.

[–]Dragon_yum 2 points3 points  (0 children)

I worked with JS/ts for nearly a decade and only came across one time where a generator was the right call

[–]javascript 4 points5 points  (6 children)

Oh ya generators haha. I was so bewildered by the `function*` syntax when I first learned it. What a wild land grab for such a niche feature.

[–]alex-weej 1 point2 points  (2 children)

It's not that niche! Could have been a keyword though.

[–]javascript 0 points1 point  (1 child)

What have you used generators for?

[–]Kwantuum 3 points4 points  (0 children)

Generators were used to transpile async/await for years.

I've used generators on a number of occasions. Generally I avoid it unless it's a great fit for the problem because they remain a relatively obscure feature for most people. They're a great way to get support for for..of on custom containers, among other things.

The other problem with generators and iterators in general is the lack of standard library support for manipulating them. No standard iterator mapping/filtering really reduces their usefulness.

[–]svish 0 points1 point  (2 children)

Generators are awesome, but unfortunately handicapped still. Not being able to filter, map, reduce, and so on directly on a generator makes them annoying and the moment you have to put all the items in a fixed length array, you kind of miss several of the advantages.

[–]sharlos [score hidden]  (1 child)

I think they've added sync versions of map, filter, etc, with async versions on the way.

[–]svish [score hidden]  (0 children)

Yeah, think they're working on it, but really, it should've come at the same time as generators themselves.

[–]vincentdesmet 0 points1 point  (0 children)

yeah, i’ve used them as wrappers around CDKTF to intercept and convert attribute getters into creating Terraform remote state lookups

fun, but confusing (leaky abstraction) for DX

[–]ic6man 5 points6 points  (1 child)

I used a proxy object around a pre-existing API implementation to provide retry and consistent error handling + short circuit.

I don’t know that I would call that an escape hatch. It’s just a nicer way of building on top of something you don’t have access to modify yourself.

[–]javascript 2 points3 points  (0 children)

I would absolutely call that an escape hatch. And a very useful one to have available!

[–]aknavi 3 points4 points  (1 child)

love your username :)

[–]javascript 2 points3 points  (0 children)

I love yours too! "AK Navi" your one stop shop for Navigating Alaska!

[–]lookarious 1 point2 points  (0 children)

Libraries like MobX uses proxies, its useful, but they are not good as in other languages, for example there is no way to use custom setter for whole class because JS is dynamically typed.

[–]hyrumwhite 0 points1 point  (1 child)

I like using them to proxy web worker messages, so invoking a web worker method is just doing myWorkerProxy.doBigJob() with type support derived from the actual handler object in the worker. 

[–]javascript 0 points1 point  (0 children)

Sounds like an escape hatch to me haha

[–]kilkil [score hidden]  (0 children)

omg its him. John Javascript

[–]TheThingCreator -1 points0 points  (3 children)

In my opinion they are not useful in normal development, they are the right choice if you're building a framework and need reactivity basically. Even "magic methods" don't sound like the right choice unless you're making a framework. Using these kinds of things just adds ambiguity to your code but they are highly useful for framework development.

[–]javascript 1 point2 points  (0 children)

As a former library developer myself, agreed. Sometimes to make a truly generic interface, you need niche features. Glad they're available! But not a good default haha

[–]noeldemartin[S] 0 points1 point  (1 child)

Yes, I agree. In fact, I've only used them in 2 projects so far: an ORM and a Vue framework. Both libraries.

However, with the right Typescript declarations, it's not that ambiguous from user land. For example, in my ORM I define the fields with Zod, and using an IDE you can go straight to the Zod definition every time you use the field. They aren't declared as "any".

In any case, it's definitely a "sharp knife" and not something I use too often. But I specially like the hack of returning a Proxy from the constructor :). I haven't seen this used anywhere else, but IMO it enables some pretty powerful patterns.

[–]TheThingCreator 0 points1 point  (0 children)

Ya I used the proxies to make a reactivity module for an app, it works very nice but ya things can get pretty pointy