codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

56 / 65 topics
50JavaScript Template Literals51JavaScript Destructuring52JavaScript Spread & Rest Operators53JavaScript Default Parameters54JavaScript Modules (import/export)55JavaScript Iterators and Generators56JavaScript Proxies
Tutorials/JavaScript/JavaScript Proxies
🌐JavaScript

JavaScript Proxies

Updated 2026-05-12
30 min read

JavaScript Proxies

In the realm of modern JavaScript, Proxies stand as a powerful tool for intercepting and customizing fundamental object operations. Whether you're building complex applications, debugging, or optimizing performance, understanding how to use proxies can significantly enhance your development capabilities. This tutorial will guide you through the basics of proxies, their syntax, common use cases, and best practices.

Introduction

JavaScript Proxies allow you to define custom behavior for basic operations on objects. These operations include property lookup, assignment, enumeration, function invocation, etc. By using proxies, you can intercept these operations and execute custom logic before or after the operation is performed. This feature is particularly useful for implementing advanced patterns like access control, logging, validation, and more.

Core Content

What is a Proxy?

A Proxy is an object that wraps another object (target) and allows you to define custom behavior for fundamental operations on the target object. The proxy object can intercept and redefine these operations, providing a way to add or modify their behavior without altering the original object.

Syntax

The basic syntax for creating a Proxy is as follows:

JavaScript
1const handler = {
2 // Define trap methods here
3};
4
5const proxy = new Proxy(target, handler);
  • target: The target object that you want to wrap.
  • handler: An object whose properties are functions defining the behavior of the proxy when an operation is performed on it.

Example: Basic Proxy

Let's start with a simple example where we create a basic proxy that logs every property access:

JavaScript
1const target = {
2 name: 'Alice',
3 age: 30
4};
5
6const handler = {
7 get(target, prop, receiver) {
8 console.log(`Accessing ${prop}`);
9 return Reflect.get(target, prop, receiver);
10 }
11};
12
13const proxy = new Proxy(target, handler);
14
15console.log(proxy.name); // Output: Accessing name
16 // Alice
Output
Accessing name
Alice

In this example:

  • The get trap is defined in the handler to intercept property access.
  • Reflect.get is used to get the value of the property from the target object.

Common Trap Methods

Proxies support several trap methods that allow you to customize various operations. Here are some commonly used traps:

Trap MethodDescription
get(target, prop, receiver)Intercepts getting a property value.
set(target, prop, value, receiver)Intercepts setting a property value.
has(target, prop)Intercepts the in operator.
deleteProperty(target, prop)Intercepts the delete operator.
apply(target, thisArg, argumentsList)Intercepts function calls.
construct(target, argumentsList, newTarget)Intercepts the new operator.

Example: Customizing Property Access

Let's create a proxy that checks if a property exists before accessing it:

JavaScript
1const target = {
2 name: 'Bob',
3 age: 25
4};
5
6const handler = {
7 get(target, prop) {
8 if (prop in target) {
9 return target[prop];
10 } else {
11 throw new Error(`Property ${prop} does not exist.`);
12 }
13 }
14};
15
16const proxy = new Proxy(target, handler);
17
18console.log(proxy.name); // Output: Bob
19console.log(proxy.age); // Output: 25
20
21try {
22 console.log(proxy.address);
23} catch (e) {
24 console.error(e.message); // Output: Property address does not exist.
25}
Output
Bob
25
Property address does not exist.

In this example:

  • The get trap checks if the property exists in the target object before returning its value.
  • If the property does not exist, it throws an error.

Practical Use Cases

Proxies have numerous practical applications. Here are a few examples:

1. Access Control

You can use proxies to enforce access control rules on objects. For instance, you might want to restrict access to certain properties based on user roles.

JavaScript
1const target = {
2 name: 'Charlie',
3 age: 35,
4 secret: 'This is a secret'
5};
6
7const handler = {
8 get(target, prop) {
9 if (prop === 'secret') {
10 throw new Error('Access denied');
11 }
12 return target[prop];
13 }
14};
15
16const proxy = new Proxy(target, handler);
17
18console.log(proxy.name); // Output: Charlie
19try {
20 console.log(proxy.secret);
21} catch (e) {
22 console.error(e.message); // Output: Access denied
23}
Output
Charlie
Access denied

2. Logging

Proxies can be used to log every property access and modification, which is useful for debugging.

JavaScript
1const target = {
2 name: 'David',
3 age: 40
4};
5
6const handler = {
7 get(target, prop) {
8 console.log(`Accessing ${prop}`);
9 return Reflect.get(target, prop);
10 },
11 set(target, prop, value) {
12 console.log(`Setting ${prop} to ${value}`);
13 return Reflect.set(target, prop, value);
14 }
15};
16
17const proxy = new Proxy(target, handler);
18
19console.log(proxy.name); // Output: Accessing name
20 // David
21
22proxy.age = 41; // Output: Setting age to 41
Output
Accessing name
David
Setting age to 41

3. Validation

Proxies can enforce validation rules when setting properties.

JavaScript
1const target = {
2 name: 'Eve',
3 age: 28
4};
5
6const handler = {
7 set(target, prop, value) {
8 if (prop === 'age' && typeof value !== 'number') {
9 throw new Error('Age must be a number');
10 }
11 return Reflect.set(target, prop, value);
12 }
13};
14
15const proxy = new Proxy(target, handler);
16
17proxy.age = 29; // Valid
18try {
19 proxy.age = 'thirty'; // Invalid
20} catch (e) {
21 console.error(e.message); // Output: Age must be a number
22}
Output

In this example:

  • The get trap logs every property access and wraps function properties to log their calls.
  • The set trap logs every property modification.

Summary

Key ConceptsDescription
ProxyAn object that wraps another object (target) and allows you to define custom behavior for fundamental operations on the target.
Trap MethodsFunctions in the handler object that intercept specific operations like get, set, has, etc.
Common Use CasesAccess control, logging, validation, etc.

Understanding JavaScript Proxies can greatly enhance your ability to manipulate and optimize object interactions in your applications. By customizing these fundamental operations, you can implement advanced patterns and improve the robustness of your code.

What's Next?

Now that you have a solid understanding of JavaScript Proxies, the next topic will introduce you to JavaScript DOM Introduction. This will take you into the realm of manipulating HTML documents using JavaScript, allowing you to build dynamic web applications. Stay tuned!


PreviousJavaScript Iterators and GeneratorsNext JavaScript DOM Introduction

Recommended Gear

JavaScript Iterators and GeneratorsJavaScript DOM Introduction