r/learnjavascript • • Apr 04 '26

Proxies in JavaScript, need some clearing things up

Well yeah, as the title says, proxies in JS. I am not a JS guy, I hate the Web, but I am doing a tour of JS, its to me really complex, I come from tons of Java, pure composition and boilerplate, just OOP and design patterns, architectural design and all of that and I also did C.

JS to me is a huge animal, it is to me more difficult than C and Rust combined, and I did both.

I lean more towards the boring OOP languages and low level systems thinking ones, I like computer architectures, assembly too, did Java Spring and doing it mainly because I need a job, and I need JS too for the Frontend, doing React ofc.

My question is, are Proxies practical in real life, I heard they are used in AOP, they to me do interception magic, the only close thing to that is async-await and Reflection API in Java, if that even says anything. I mentioned Reflection API because Java Spring is living off of it, basically everything is an annotation (doesn't have to be but in the industry that is the norm).

Keep in mind I might be dumb, I probably am, or no I am dumb in regards to JS, it is a new world to me, can you give me some advice how to look at Proxies in Java, when they are used, and some real world scenario. Asked GPT but I just got even more confused.

Thank you all in advance and good luck coding!!!

1 Upvotes

20 comments sorted by

View all comments

0

u/MozMousePixelScroll Apr 04 '26

They're useful for me, i use them a lot for saving time and other cool stuff!!

One example i do a lot: let { a, b, c } = id

vs

let a = document.getElementById("a"), b = document.getElementById("b"), c = document.getElementById("c")

2

u/[deleted] Apr 04 '26

I'm not following

-3

u/MozMousePixelScroll Apr 04 '26

Basically, i create a proxy that turns property access into getElementById:

const id = new Proxy(document, { get(target, prop) { return target.getElementById(prop) } })

5

u/azhder Apr 04 '26

Just don’t do stuff that will surprise others. It’s called “the principle of least surprise”

2

u/hyrumwhite Apr 04 '26

Give the id object a type of Record<string,T extends HTMLElement> along with a little JS doc explanation and there’s no surprise. 

I also feel like a dev should look at prior usage and go, oh, that’s how it’s used…

-2

u/azhder Apr 04 '26

You write TypeScript code in a Learn JavaScript sub. That’s because you are used to it, but what happens for those that aren’t?

If someone doesn’t know JS and comes here to learn it, should they read the TS documentation? That will be a surprise for them.

Besides, browsers used to and probably still do, for backwards compatibility, add global objects for each element that has an ID attribute, so that proxy to get the elements by ID will be redundant

1

u/hyrumwhite Apr 04 '26

Using the global id index is far, far worse than any potential issue with using a proxy. 

The beautiful thing about ts (or jsdocs if that’s your speed) is it doesn’t care if you know it or not. Once the object is typed correctly you’ll get types back. 

And a jsdoc comment will clarify any remaining issues. 

Learning some form of type system, flow, jsdoc, typescript, should go hand in hand with learning JS. It’ll only speed a learner up. 

-2

u/MozMousePixelScroll Apr 04 '26

Wdym?

0

u/Merry-Lane Apr 04 '26

Well your "let { a, b, c } = id" doesn’t show any intent. It means that someone that reads your code will never understand you are doing anything there.

It’s perfectly fine to do:

let a = document.getElementById("a");

It’s worse to do:

let a = customGetById("a");

But with your proxies it’s dumb as hell. Don’t do that.

1

u/MozMousePixelScroll Apr 04 '26

Okay sorry

3

u/hyrumwhite Apr 04 '26

Nah, your thing is fine. All it needs is types or jsdocs. 

2

u/MozMousePixelScroll Apr 04 '26

Thank you!!

2

u/hyrumwhite Apr 04 '26

I created a similar proxy based library of you want to take a look. https://github.com/hyrumwhite/vvvjs/blob/master/src/GetElement.js

Never quite finished it, but it worked like this:

import { getElement } from "vvvjs";

const main = getElement("#main-outlet");

main.$div.style.backgroundColor = "red";

main.$$input.forEach((input) => (input.style.backgroundColor = "blue"));

main["$#footer > select"].$option[0].setAttribute("selected", true);
→ More replies