Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Dates JS Arrays JS Typed Arrays JS Sets JS Maps JS Math JS RegExp JS Data Types JS Errors JS Events JS Programming JS References JS Versions

JS Advanced

JS Functions JS Objects JS Classes JS Iterations JS Asynchronous JS Modules JS Meta Programming JS HTML DOM JS Windows JS Web API JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Proxy

What is a Proxy?

A Proxy is a special object that wraps another object and lets you intercept and control operations performed on that object.

A Proxy is a "guard" ("middleman") between your code and a real object.

A Proxy lets you run your own code whenever someone interacts with an object.

What Can a Proxy Do?

A Proxy can intercept when someone (or something) 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)

Proxy Example

Example

// 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";
let text = proxy.name;
Try it Yourself »

Reflect with Proxy (Very Common)

Proxy lets you intercept operations on objects:

  • Read a property
  • Write a property
  • Delete a property
  • Check property existence

Example

// Create an Object
const user = { name: "Jan", age: 40 };

// Create a Proxy
const proxy = new Proxy(user, {
  get(target, prop) {
    // safe forwarding
    return Reflect.get(target, prop);
  },
  set(target, prop, value) {
    // safe forwarding
    return Reflect.set(target, prop, value);
  }
});
Try it Yourself »

Why Proxies?

Proxies allow you to:

  • Add logging
  • Validate changes
  • Create reactive systems (like Vue.js)
  • Auto-generate properties
  • Protect sensitive data
  • Create virtual or computed objects
  • Intercept function calls

Proxy Traps

Trap NameTrigger
getReading a Property
setWriting a Property
has"prop" in obj is used
deletePropertyDeleting a Property
applyCalling a Function
constructnew is used


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.