Proxy Pattern in JavaScript – A Practical Guide

Yasir MYasir M
3 min read

Have you ever wanted to control access to an object, log what's happening under the hood, or validate how data is set? That’s where the Proxy Pattern comes into play.

In this guide, we’ll break down the Proxy Pattern in a simple way, walk through real-world examples, and write clean JavaScript code to understand how it works.


📌 What We'll Cover

  1. What is the Proxy Pattern?

  2. When and why should you use it?

  3. How JavaScript’s Proxy object works

  4. Real-world practical examples:

    • Access control (auth logic)

    • Logging and analytics

    • Data validation

  5. Final thoughts


🤔 What is the Proxy Pattern?

The Proxy Pattern is a structural design pattern that acts as a substitute or placeholder for another object. It allows you to control access, add extra behavior, or defer execution without modifying the original object.

In JavaScript, we implement this using the built-in Proxy constructor.


🧠 Why and When to Use It?

Here are common situations:

  • You want to restrict access based on conditions (e.g., role-based access).

  • You want to log every interaction with an object.

  • You want to validate or sanitize data before saving it.

  • You want to create lazy-loading behavior (fetching data only when needed).


⚙️ Basic Proxy in JavaScript

const user = {
  name: "Yasir",
  age: 22
};

const proxyUser = new Proxy(user, {
  get(target, prop) {
    console.log(`Accessing ${prop}`);
    return target[prop];
  },
  set(target, prop, value) {
    console.log(`Setting ${prop} to ${value}`);
    target[prop] = value;
    return true;
  }
});

proxyUser.name;       // Logs: Accessing name
proxyUser.age = 25;   // Logs: Setting age to 25

🔍 This is a simple proxy that logs whenever a property is accessed or modified.


✅ Real-World Example 1: Role-based Access Control

const confidentialData = {
  salary: "₹1,20,000",
  department: "Engineering"
};

function createAccessProxy(userRole) {
  return new Proxy(confidentialData, {
    get(target, prop) {
      if (userRole !== "admin") {
        return "Access Denied";
      }
      return target[prop];
    }
  });
}

const employeeView = createAccessProxy("employee");
const adminView = createAccessProxy("admin");

console.log(employeeView.salary); // Access Denied
console.log(adminView.salary);    // ₹1,20,000

👨‍💼 This pattern is super useful when building dashboards or admin panels where data access depends on user roles.


📝 Real-World Example 2: Logging Object Usage

function createLogger(obj) {
  return new Proxy(obj, {
    get(target, prop) {
      console.log(`[LOG] Accessed property: ${prop}`);
      return target[prop];
    }
  });
}

const settings = {
  theme: "dark",
  fontSize: "16px"
};

const loggedSettings = createLogger(settings);

loggedSettings.theme;    // [LOG] Accessed property: theme
loggedSettings.fontSize; // [LOG] Accessed property: fontSize

📈 Great for debugging or adding analytics to user preferences.


🔐 Real-World Example 3: Validating Data Before Setting

const product = {
  name: "Phone",
  price: 10000
};

const validatedProduct = new Proxy(product, {
  set(target, prop, value) {
    if (prop === "price" && value < 0) {
      throw new Error("Price cannot be negative");
    }
    target[prop] = value;
    return true;
  }
});

validatedProduct.price = 12000; // ✅ OK
validatedProduct.price = -5000; // ❌ Error: Price cannot be negative

✅ This is super helpful for form validation or handling input from external sources.


📦 Summary

  • The Proxy Pattern helps you control access, add custom logic, and wrap behavior around objects.

  • It’s very useful in cases like:

    • Access control (RBAC)

    • Logging for debugging or analytics

    • Input/data validation


🚀 Final Thoughts

The Proxy Pattern gives you powerful control over how objects behave — without needing to change their original structure. Whether you're building admin dashboards, setting up logs, or validating data, proxies can make your code more secure, clean, and flexible.

Use it wisely – and your code will thank you!


If you liked this guide, feel free to connect with me on LinkedIn or drop your thoughts below! ✌️

0
Subscribe to my newsletter

Read articles from Yasir M directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yasir M
Yasir M