What is a proxy in javascript

What is a proxy in javascript

Date: 30/03/2020

Introduction

Proxy in javascript is used to define custom behaviour for fundamental operations. We will have a proxy object where we will define our desired behaviour and wrap the object with proxy API.

Basic Example

const handler = {
  get: function(obj, prop) {
    return prop in obj ?
      obj[prop] :
      37;
  }
};

const p = new Proxy({}, handler);
p.a = 1;
p.b = undefined;

console.log(p.a, p.b); 
//  1, undefined

console.log('c' in p, p.c); 
//  false, 37

The above example has an object that has a getter method. It acts as a getter method with setter only if the property doesn’t exist.

Syntax

const p = new Proxy(target, handler)

It has method revocable. The Proxy.revocable() method is used to create a revocable Proxy object.

We can also use methods such as set, apply in the handler and write our rules based on need and wrap it with Proxy API.
Check here for more info here https://javascript.info/proxy

Conclusion

Thanks for using pheonixsolutions. Share it with your friends to keep it alive if you find it useful.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Comment

Scroll to Top