Actual use case for JavaScript PROXY!
Vložit
- čas přidán 21. 09. 2021
- Use cases for JavaScript proxies is one of the things most people aren't familiar with doing, but it's a super powerful tool if you know how to use them.
I show you a way I'm using proxies today at work for deprecating object properties that aren't functions.
First js vido in my life that i actually enjoyd.
Very nice tutorial! Definitely an underrated channel..
Great video! I've found of a use-case for proxies before, but this seems really useful.
Awesome video Kevin! It is a very helpful use-case. Thank you so much for this
ONE OF THE BESTEXPLANATION
THABK YOU❤
Amazing video, you just earned yourself a new subscriber!
This is super helpful, thank you
This was a great example and use case, thank you!
I'll definitely share this one, truly amazing content.
Great explanation, thanks
Great video, thanks for the info!
Great Video ! Thanks SIr !
Nice video.
It is inaccurate to say the original commits are “deleted” when performing a rebase. They still exist and can be accessed and even recovered if you know their original hashes. They do end up in an orphaned state and can be “cleaned up” under certain circumstances in the future.
Great Vedio, very helpful
Thx very good video
For me it seems more obvious and simple just to add to the `fontSized` object a property `__proto__: legacyFontSizes`. Then `extreaLagre` and `extraSmall` won't appear by iteration, but not break the legacy code as e.g. with an attempt of getting `extraSmall` the `gigantic` will be returned.
I still find your video very clear-cut and helpful, thank you!
But can you please answer if in this particular case my solution is inferior to the approach with `Proxy`?
That's a great idea and a lot simpler! Personally, I wouldn't depend on proto as that's like black magic; unless I'm missing the mark. It's got that code smell. You also need to check for existing values first too. Hmm 🤔.
Can I ask what font do you use in the video?
For the code of thumbnail? The code is Victor Mono, but I'm not exactly sure what it was in this video.
You can literally just set the forntsizes extralarge to the new translation. The proxy isnt doing anything magical.
FontSizes.ExtraLarge.Value
When you have to update your code for depricated values you can just set the value from the object. What is it that a proxy can do that cant be done in a simple return value or if then statements?
If people are using your library, but you have an object and not a function, this is a way of adding code to an object that isn't a function.
Most times, you don't want breaking changes in a library, and proxies can make these changes non-breaking.
@Kevin Ghadyani - JavaScript oh you're using it inside a library. I was thinking this was all inside the same class. I guess if it's in a library then it probably is more useful as the Contractor is created for 100's of users.
Why we can't do something like
const FontSize = {
.......
tiny: tiny,
get extraSmall() {
console.warn('warning...')
return this.tiny
}
.......
}
Does that work? I've only ever done that with proxies and Object.defineProperty.
I feel this should work, but with these caveats:
1. The more the number of deprecated keys, the larger the object grows as you need to define getters/setters for all the deprecated keys. Proxy does it in one place.
2. Object.keys(FontSize) would still return the deprecated keys. While you can mark the deprecated keys as non-enumerable, you would need to do that for each and every key.
I had a live stream where I converted object-based to proxies, and it's like you said, proxies scale better.