avatarDeepak Gupta

Summary

The provided web content discusses JavaScript Proxy, an ES6 feature that allows for the creation of objects that can monitor and intercept operations on other objects, providing a powerful tool for meta-programming, validation, API call wrapping, and more.

Abstract

JavaScript Proxy is a sophisticated feature introduced in ES6 that enables developers to create a wrapper around an object or function, known as the target, which can then be monitored and modified through a handler object. This handler object defines traps, which are functions that intercept and customize the behavior of fundamental operations on the target. The article explains the key concepts of targets, handlers, and traps, and provides examples illustrating how Proxy can be used for object property validation, API call wrapping, and other advanced use cases. The author emphasizes the versatility of Proxy for tasks such as value correction, tracing property accesses, and monitoring asynchronous functions, suggesting that it is a valuable yet underutilized feature in JavaScript development.

Opinions

  • The author believes that JavaScript Proxy is a powerful and underappreciated feature that offers meta-programming capabilities similar to those found in other programming languages.
  • Proxy is considered "magic methods" due to its ability to intercept and define custom behavior for JavaScript object operations.
  • The article suggests that Proxy can greatly simplify complex tasks like validation, API call wrapping, and monitoring asynchronous functions.
  • The author finds JavaScript Proxy particularly useful in the context of RESTful web services, validation, and monitoring asynchronous functions, and expresses an intention to explore its potential further.
  • A side note indicates that the examples provided in the article could be optimized, implying that there is room for improvement and efficiency in using Proxy.

Javascript — Proxy

Recently, I was reading about some less famous Javascript features and I found Javascript Proxy.

Before going into why this is important, let's understand what is it. If you already know, feel free to skip a few paragraphs.

Proxy are magic methods

Proxy is an object in javascript which wraps an object or a function and monitors it via something called target. Irrespective of the wrapped object or function existence. Proxy are similar to meta programming in other languages.

There are 3 key terms we need to understand before we proceed:

  1. Targets: Object or Function to be proxied.
  2. Handler: The function that does something on Object or Function that is proxied.
  3. Traps: These are some functions used to work on targets. Click here to read more about traps.

Below is how we defined it

Syntax

We use Proxy Class from ES6, the arguments, the target is the wrap object and handler will be the function used to do some action on target using traps.

Below is a simple example of its usage

Basic example for proxy

We made 2 objects called target and handler, target is a simple object with a message key and handler is an object that has a get key with a function associated with it. We pass 2 objects in proxy class and in return receive a proxy object by which we can access the message property of the target object.

Here is a small example of how to use it for validation of object values

Validation of object key’s

We use an empty object as a target object and a handler that takes set a trap on the target object and do the validation. Pretty easy right!

Let's look into how we can use it to make API call wrapper.

We have used axios, and then created an instance with our base URL, created a handler for a proxy that returns an object of get, post, put, delete, patch function to be used, and at last made an API object which is a proxy object to an empty object target.

We can then use it as :

Calling API proxy object

This can be extended doing validation, value correction, tracing property accesses, warning about unknown properties, negative Array indices, data binding, accessing a restful web service (method calls), revocable references, monitoring async functions, type checking and much more, Read here more

I personally found javascript proxy very use-full in restful web service, validation, monitoring async functions. I will explore more.

Side note: The examples used can be optimized.

You may also like my other articles

  1. Javascript Execution Context and Hoisting
  2. Javascript — Generator-Yield/Next & Async-Await 🤔
  3. Understanding Javascript ‘this’ keyword (Context).
  4. Javascript data structure with map, reduce, filter
  5. Javascript- Currying VS Partial Application
  6. Javascript ES6 — Iterables and Iterators
  7. Javascript — Scopes

If you like this article, please feel free to share and help others find it!

Thank you!

JavaScript
ES6
Technology
Programming
Web Development
Recommended from ReadMedium