Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Major use cases for ES6 proxies

I recently got to know about ES6 proxies but I don't see a good reason to use it. I mean, everything that one could do with Proxy can be done without it, except if I'm missing something.

For example, most folks talk about validation when it comes to proxy but one could apply some JS goodness to validate and everyone is fine. I would appreciate if someone could open my eyes to some major use cases of Proxies. Thanks!

like image 231
Ademola Adegbuyi Avatar asked Apr 23 '17 12:04

Ademola Adegbuyi


People also ask

What is use of proxies in ES6?

ES6 implements intercession form of meta programming using Proxies. Similar to ReflectAPI, the Proxy API is another way of implementing meta programming in ES6. The Proxy object is used to define custom behavior for fundamental operations. A proxy object performs some operations on behalf of the real object.

What is JavaScript proxy used for?

In JavaScript, proxies (proxy object) are used to wrap an object and redefine various operations into the object such as reading, insertion, validation, etc. Proxy allows you to add custom behavior to an object or a function.

Why proxies in JavaScript are fantastic?

The Proxy object enables you to wrap the target object and by doing that we can intercept and redefine fundamental operations for that object. Basically, it means that we are going to take an object, wrap it with a Proxy which will allow us to create a “hidden” gate, and control all access to the desired object.


3 Answers

I mean, every thing that one could do with Proxy can be done without it...

That isn't remotely true.

Consider the common need to catch access to properties that don't exist:

const o = {foo: "bar"};
console.log(o.blarg);

It's common to want to handle that in a way other than the default, which is to log undefined. Proxy lets us do that, via the get trap. Example:

const o = {foo: "bar"};
const p = new Proxy(o, {
  get(target, prop, receiver) {
    return prop in target ? target[prop] : "nifty!";
  }
});
console.log(p.foo);
console.log(p.blarg);

Another example is the ability to hook into the various operations that get the list of properties on an object. There is no way to hook into that without Proxy. With Proxy, it's easy: You use the has trap or the ownKeys trap depending on what you want to hook into.

In terms of other use cases: Proxy is the ultimate tool for implementing the Facade pattern. Look for the use cases of Facade, and you'll find use cases for Proxy.

like image 177
T.J. Crowder Avatar answered Oct 22 '22 20:10

T.J. Crowder


There is actually lots you can do with it. Theres an awesome github repo where this guy put together a bunch of proxy resources which you can check out.

https://github.com/mikaelbr/proxy-fun

Also, check out my gists, I recently started playing around with proxies, and I have a couple examples that are pretty unique. You can essentially build your own DSL using proxy and program in a closer fashion to the way you think.

https://gist.github.com/jasuperior

like image 4
Ja Superior Avatar answered Oct 22 '22 18:10

Ja Superior


Proxies represent a class of dynamic programming (as in dynamic languages, not the method of problem solving) called metaprogramming, and it is absolutely not the case that anything that can be done with proxies can be done without them. In fact that's really the reason proxies exist: to enable entirely new capabilities that weren't possible before.

Proxies enable you to intercept operations on your objects that would have otherwise been purely the responsibility of the JavaScript engine; property accessing and mutating being the two obvious examples.

T.J.'s answer is a good example of something you can't do without proxies. To give you another, I am using proxies to enable singleton instances of objective entities to allow their backing data stores to be swapped-out and replaced with entirely new objects, without affecting the references that are pointing to those objects.

To do this without proxies, we would have to iterate over each field of the object and swap them out for the new fields in the new object. While it's true JavaScript is dynamic enough to allow that to be possible, Proxies allow it to be solved in a much more elegant way: the hidden backing store of the proxy is simply replaced with the new object and all future property accesses are simply directed to the new backing store rather than the old one, while external references to the object (which is actually the proxy) need be none the wiser. To them, it appears as though it is the same object (because it is), but now it happens to have completely different data behind it.

This is only one example of what you can use Proxies for. They are really quite powerful because of how dynamic they are. I'm just getting to know them, but already I can say I'm quite in love. :)

like image 3
devios1 Avatar answered Oct 22 '22 20:10

devios1