I have read the documentation for Proxy and Reflect on MDN but didn't understand much. Can anyone please explain or mention the real world use of t...
For further actions, you may consider blocking this person and/or reporting abuse
I posted this elsewhere, but here's an example use-case:
Let's say you want to use arrays as accessors, like this:
You can use
Proxy
andReflect
to achieve it:Here,
Reflect
is being used to take care of the "default" case of accessing an array using a normal numerical index.The rest of the time we're abusing the fact that any
key
inarr[key]
form gets coerced into a string, so can be parsed into whatever we like once it reaches the getter/setter in aProxy
handler.this looks very helpful
I’m not too familiar with Reflect, but have a pretty good analogy for proxies that I’m sure will be helpful
If you know about Express and how the whole middleware pattern works, then proxies are exactly like middleware but for plain JavaScript objects. So, whenever you read from and object or make any changes to an object you can execute custom behaviour on those operations.
And as for the use cases, the only real ones I can think of is when you’re building some framework.
really very good example 👌
First time I came across
Reflect
was in the web components native shim:MDN:
Reflect.construct()
.Browsers implement custom elements as native classes (separate from JS classes).
Some tooling insists on compiling down to ES5 and using
Reflect.construct()
instead ofnew
makes it possible to use custom elements from ES5 code (obviously the browser has to support custom elements which implies that it also supports classes (ES2015)).a good one 👍
I wrote a tiny package that uses proxy to create factories for any of your existing es6 (or old es5) classes and allows to define private fields or function based on a rule function: github.com/jankapunkt/js-class-pri...
No need to fully rewrite the classes to support private members (plus I still dislike the choice of the hash character for the private fields feature)
well, hard to imagine a use case for that unless you're building your own framework or a library. If someone asks me such stuff in an interview, I would go 'cmon are you doing some rocket science'?)
Right, I for example use proxies for reactivity in the front-end framework I build.
But there's this point I force myself to learn anything and everything about JavaScript just to convince myself I'm good at it :/.
🙃🙃
Once I used Reflect on a jest test to simulate the class/ method spyOn
Can you please share code snippet if possible.
I was using typeorm + nestjs + jest so it has a queryBuilder and I wanted that my custom queryBuilderMethod "inherits" some default queryBuilder methods, because in that case, I was using the default plus the custom.
So my other custom methods that depends on those configuration custom + default will have the right params and return the query as intended
You may find this helpfull - use-es6-proxies-to-enhance-your-ob...
Thanks man.
man these are really very helpful 🤘 thanks