bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JavaScript Proxy

Concept visual

JavaScript Proxy

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

What is a Proxy?

A Proxy is a JavaScript object that can wrap other objects. A Proxy lets you control operations on other objects. A Proxy can trap and intercept code when someone is: Reading a property (get) Setting a property (set) Deleting a property (deleteProperty) Checking if a property exists (has) Calling a function (apply) Constructing an object (construct) A Proxy lets you run your own code when someone interacts with an object. A Proxy gan be a middleman between your code and a JavaScript object.

Proxy Syntax const proxy = new Proxy(target, handler);

Formula

target - the original object or function handler - an object with trap methods

Example

const myObject = {name: "Jan"};
const proxy = new Proxy(myObject, {
get(target, prop) {
return target[prop];
}
});

Proxy Logging

A typical Proxy example is logging of object changes.

Below is a demo that:

Wraps an object in a Proxy

Logs whenever a property is read or written Logs each get and set operation in real time

Example

Log all changes to all property values: // Create an Object const user = { name: "Jan", age: 40 };

//Create a Proxy const proxy = new Proxy(user, {
get(target, property) {
log("Getting: " + property);
return target[property];
}, set(target, property, value) {
log("Setting: " + property);
return target[property];
}
});
proxy.name = "John";
proxy.age = 42;
let text1 = proxy.name;
let text2 = proxy.age
Proxy with Reflect (Most Common)

Below is a demo that:

Wraps an object in a Proxy

Logs whenever a property is read or written Uses Reflect.get() and Reflect.set() inside the Proxy handlers get trap uses Reflect.get(target, property, receiver) set trap uses Reflect.set(target, property, value, receiver) Reflect makes the Proxy behavior match the normal object behavior

Example

Log all changes to all property values: // Create an Object const user = { name: "Jan", age: 40 };

// Create a Proxy const proxy = new Proxy(user, {
get(target, property) {
log("Getting: " + property);
    // safe forwarding return Reflect.get(target, property);
}, set(target, property, value) {
log("Setting: " + property);
    // safe forwarding return Reflect.set(target, property, value);
}
});
proxy.name = "John";
proxy.age = 42;
let text1 = proxy.name;
let text2 = proxy.age

Proxy with Reflect Flow

Formula

The flow below is the essence of JavaScript metaprogramming with Proxy + Reflect.

Flow Explanation:

Your code interacts with an object

Proxy intercepts the operation

Your trap code decides what to do

Reflect forwards the operation safely

The target object receives the real action

Why Proxies?

Proxies allow you to:

Add logging

Validate changes

Auto-generate properties

Protect sensitive data

Create virtual or computed objects

Intercept function calls

Create reactive systems (like Vue.js)

Previous

JavaScript Reflect

Next

Reflect & Proxy Reference