Sitemap

Proxy Objects in Javascript!!!

Helllooooo everyone,

Today we will be diving deep into a very interesting concept of javascript, i.e. Proxy Object. Post-reading this article you will have a thorough understanding of what objects are and how they work in javascript, Stay tuned!!!!

Dracarys!!!!

Step 1: Consider a simple javascript object, “Mango” with 2 properties associated with it, as shown below.

Press enter or click to view image in full size

Step 2: Every object in javascript has a few default functions associated, one of which is the “get” function. This function is called internally as soon as we try accessing any key from the object.

Press enter or click to view image in full size

The above code will give the following output,

Press enter or click to view image in full size

In the above console.log statements, when we try to access Mango.color or Mango.rate, the “get” function is invoked internally. Isn’t this interesting, how javascript treats objects as functions internally, But this is a topic for another hour.

Step 3: Proxy Object gives us a way to override the default functions (get, set, has, etc) associated with an object.

Get Siddesh Thorat (Software Development Enthusiast)’s stories in your inbox

Join Medium for free to get updates from this writer.

The proxy object can be created with a Proxy class in javascript, the following is the syntax for it.

Press enter or click to view image in full size

Consider the following example to understand this better,

Press enter or click to view image in full size
  • In the above snippet, we have created a “proxyMango” Object, and we are overriding the default “get” function, with our custom function.
  • The proxy class takes two arguments, the first is the Mango object, and the second is the object containing override functions. In the above case, it’s the get function that we are overriding.

The output of the above snippet is as follows,

Press enter or click to view image in full size

when we try accessing the color property of the original Mango Object, it calls the default get function and hence we get the output “Old MangoObject yellow”
but when we try accessing the color property of the proxyObject, it calls our “custom get” function, hence we see “Get function accessed — — — — →” getting printed before the final output i.e. “New MangoObject yellow”

Step 4: On similar lines, we can override, set, has, etc functions for any object. This comes in handy when we want to add validations, business logic, etc on complex objects. Proxy objects are used along with Reflect. The output remains the same. Reflect object has several functions, which can come in handy when working with complex objects.

Press enter or click to view image in full size

Hope your understanding of Javascript objects and proxy objects is thorough now, Do let me know in the comments section if you need an article on any particular topic, I would be more than happy to share my views on it.

Knowledge is power!!!🥇

--

--