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

JS Reference

JS by Category JS by Alphabet

JavaScript

JS Arrays JS Boolean JS Classes JS Dates JS Error JS Global JS Iterators JS JSON JS Maps JS Math JS Numbers JS Objects JS Operators JS Assignment JS Arithmetic JS Comparison JS Logical Operators JS Bitwise Operators JS Misc Operators JS Precedence JS Promises JS Proxy JS Reflect JS RegExp Patterns JS RegExp Reference JS Sets JS Statements JS Strings JS Typed Arrays JS Typed Reference

Window

Window Object Window Console Window History Window Location Window Navigator Window Screen

HTML DOM

HTML Documents HTML Elements HTML Attributes HTML Collection HTML NodeList HTML DOMTokenList HTML Styles
alignContent alignItems alignSelf animation animationDelay animationDirection animationDuration animationFillMode animationIterationCount animationName animationTimingFunction animationPlayState background backgroundAttachment backgroundClip backgroundColor backgroundImage backgroundOrigin backgroundPosition backgroundRepeat backgroundSize backfaceVisibility border borderBottom borderBottomColor borderBottomLeftRadius borderBottomRightRadius borderBottomStyle borderBottomWidth borderCollapse borderColor borderImage borderImageOutset borderImageRepeat borderImageSlice borderImageSource borderImageWidth borderLeft borderLeftColor borderLeftStyle borderLeftWidth borderRadius borderRight borderRightColor borderRightStyle borderRightWidth borderSpacing borderStyle borderTop borderTopColor borderTopLeftRadius borderTopRightRadius borderTopStyle borderTopWidth borderWidth bottom boxShadow boxSizing captionSide caretColor clear clip color columnCount columnFill columnGap columnRule columnRuleColor columnRuleStyle columnRuleWidth columns columnSpan columnWidth counterIncrement counterReset cssFloat cursor direction display emptyCells filter flex flexBasis flexDirection flexFlow flexGrow flexShrink flexWrap font fontFamily fontSize fontStyle fontVariant fontWeight fontSizeAdjust height isolation justifyContent left letterSpacing lineHeight listStyle listStyleImage listStylePosition listStyleType margin marginBottom marginLeft marginRight marginTop maxHeight maxWidth minHeight minWidth objectFit objectPosition opacity order orphans outline outlineColor outlineOffset outlineStyle outlineWidth overflow overflowX overflowY padding paddingBottom paddingLeft paddingRight paddingTop pageBreakAfter pageBreakBefore pageBreakInside perspective perspectiveOrigin position quotes resize right scrollBehavior tableLayout tabSize textAlign textAlignLast textDecoration textDecorationColor textDecorationLine textDecorationStyle textIndent textOverflow textShadow textTransform top transform transformOrigin transformStyle transition transitionProperty transitionDuration transitionTimingFunction transitionDelay unicodeBidi userSelect verticalAlign visibility width wordBreak wordSpacing wordWrap widows zIndex

HTML Events

HTML Events HTML Event Objects HTML Event Properties HTML Event Methods

Web APIs

API Canvas API Console API Fetch API Fullscreen API Geolocation API History API MediaQueryList API Storage API Validation API Web

HTML Objects

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <tbody> <td> <tfoot> <th> <thead> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

Other References

CSSStyleDeclaration JS Conversion


JavaScript Proxy

Proxy is a JavaScript Object with methods for low-level metaprogramming.

New to Metaprogramming?

Visit Our Metaprogramming Tutorial


Proxy Syntax

const proxy = new Proxy(target, handler);
  • target - the original object or function
  • handler - an object with trap methods

Example

const user = { name: "Jan" };

const proxy = new Proxy(user, {
  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
Try it Yourself »

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
Try it Yourself »

Proxy with Reflect Flow

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

Proxy

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)

Proxy Validation

Example

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

// Create a Proxy
const proxy = new Proxy(user, {
  set(target, prop, value) {
    if (prop === "age" && value < 0) {
      text = "Age cannot be negative!";
      document.getElementById("demo").innerHTML = text;
    }
    return Reflect.set(target, prop, value);
  }
});

proxy.age = 45; // OK
proxy.age = -5; // Error
Try it Yourself »

Virtual Properties

Example

// Create an Object
const person = {
first: "John",
last: "Doe"
};

// Create a Proxy
const virtual = new Proxy(person, {
  get(target, prop) {
    if (prop === "fullName") {
      return target.first + " " + target.last;
    }
    return Reflect.get(target, prop);
  }
});

let text = virtual.fullName; // "John Doe"
Try it Yourself »

Dynamic Functions

Example

const fn = new Function("a", "b", "return a + b");

let result = fn(3, 2);
Try it Yourself »

Metaprogramming

Metaprogramming lets JavaScript:

  • Intercept behavior
  • Modify behavior
  • Define new behavior
  • Generate behavior dynamically

It gives developers deep control over the language's inner workings.


Proxy Traps

A trap is a function inside a Proxy handler.

It runs whenever a specific operation is performed on the Proxy.

Below is a complete and accurate explanation of every JavaScript Proxy trap, what triggers them, their parameters, and what they are expected to return.

Trap NameTriggered when
getA property is read
setA property is changed
hasUsing the in operator
deletePropertyA property is deleted
applyA function is called
constructAn object is cretated (with new)
getOwnPropertyDescriptorA property descriptor is retrieved
definePropertyA property is defined
getPrototypeOfA prototype is retrieved
setPrototypeOfA prototype is set
isExtensibleExtensibility is checked
preventExtensionsExistenibility is prevented
ownKeysProperties are listed

Note

The list above is 2025-accurate and includes all 13 Proxy traps defined in ECMAScript.

Each trap handler is decribed below.


handler.get()

Triggered when a property is read:

get(obj, prop, receiver) {
  return Reflect.get(obj, prop, receiver);
}

Triggered by:

proxy.property
proxy["property"]
object.property()

Parameters

  • obj - the target object
  • prop - the property being accessed
  • receiver - the this value for getters (usually the proxy itself)

Must Return

  • The property value

handler.set()

Triggered when a property is changed:

set(obj, prop, value, receiver) {
  return Reflect.set(obj, prop, value, receiver);
}

Triggered by:

proxy.property = value
proxy["property"] = value

Parameters

  • obj - the target object
  • prop - the property being accessed
  • value - the new property value
  • receiver - the this value for setters (usually the proxy itself)

Must Return

  • true - if assignment succeeded
  • false - to indicate failure

(Throwing is also allowed.)


handler.has()

Intercepts the in operator.

has(obj, prop) {
  return Reflect.has(obj, prop);
}

Triggered by:

"property" in proxy

Parameters

  • obj - the target object
  • prop - the property being accessed
  • receiver - the value of this for setters (usually the proxy itself)

Must Return

  • true
  • false

handler.deleteProperty()

Intercepts the delete operator.

deleteProperty(obj, prop) {
  return Reflect.deleteProperty(obj, prop);
}

Triggered by:

delete proxy.property

Parameters

  • obj - the target object
  • prop - the property to delete

Must Return

  • true - for sucess
  • false - for failure

handler.apply()

Triggered when a function is called:

apply(func, thisArg, args) {
  return Reflect.apply(func, this, args);
}

Triggered by:

proxy()
proxy.call()
proxy.apply()

Parameters

  • func - the callable object (function)
  • this - the this argument for the call
  • args - the function arguments array

Must Return

  • The return value of the function

handler.construct

Intercepts the new operator.

construct(obj, args, newTarget) {
  return Reflect.construct(obj, args, newTarget);
}

Triggered by:

nex proxy()

Parameters

  • obj - the constructor object
  • args - the array of arguments passed
  • newTarget - the constructor

Must Return

  • An Object (The new instance)

The construct trap only runs when you use new.

const obj = {}  // No trap
Object.create() // No trap

class User {};
new User();     // No trap

handler.getOwnPropertyDescriptor()

Intercepts property descriptor retrieval.

getOwnPropertyDescriptor(obj, prop) {
  return Reflect.getOwnPropertyDescriptor(obj, prop);
}

Triggered by:

Object.getOwnPropertyDescriptor(obj, prop)

Parameters

  • obj - the target object
  • prop - the property to describe

Must Return

  • A property descriptor
  • undefined

handler.defineProperty()

Intercepts Object.defineProperty().

defineProperty(obj, prop, descriptor) {
  return Reflect.defineProperty(obj, prop, descriptor);
}

Triggered by:

Object.defineProperty()

Parameters

  • obj - the target object
  • prop - the property to describe
  • descriptor - the property descriptor

Must Return

  • true - if sucess
  • false - if not

handler.getPrototypeOf()

Intercepts prototype lookup.

getPrototypeOf(obj) {
  return Reflect.getPrototypeOf(obj);
}

Triggered by:

Object.getPrototypeOf()

Parameters

  • obj - the target object

Must Return

  • An Object
  • null

handler.setPrototypeOf()

Intercepts setting the prototype.

setPrototypeOf(obj, prototype) {
  return Reflect.setPrototypeOf(obj, prototype);
}

Triggered by:

Object.settPrototypeOf()

Parameters

  • obj - the target object
  • prototype - the new prototype or null

Must Return

  • An Object
  • null

handler.isExtensible()

Intercepts checking if an object is extensible.

isExtensible(obj) {
  return Reflect.isExtensible(obj);
}

Triggered by:

Object.isExtensible()

Parameters

  • obj - the target object

Must Return

  • true - for yes
  • false - for no

handler.preventExtentions()

Intercepts making an object non-extensible.

preventExtensions(obj) {
  return Reflect.preventExtensions(obj);
}

Triggered by:

Object.preventExtensions()

Parameters

  • obj - the target object

Must Return

  • true - for success
  • false - for failure

handler.ownKeys()

Intercepts operations listing keys (property names or symbols).

ownKeys(obj) {
  return Reflect.ownKeys(obj);
}

Triggered by:

Object.keys()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()

Parameters

  • obj - the target object

Must Return

  • array of keys with no duplicates

Proxy Traps Map Reflect Methods

A Proxy trap represents one of JavaScript's internal operations:

  • [[Construct]]
  • [[Call]]
  • [[Get]]
  • [[Set]]
  • [[HasProperty]]
  • [[Delete]]
  • [[DefineProperty]]
  • [[GetOwnProperty]]
  • [[OwnPropertyKeys]]
  • [[GetPrototypeOf]]
  • [[SetPrototypeOf]]
  • [[PreventExtensions]]
  • [[IsExtensible]]

These internal operations are what JavaScript uses (inside the engine) when you access or modify objects.

When a Proxy intercepts one of these operations, they should be forwarded correctly:

get(target, property, receiver) {
  return Reflect.get(target, property, receiver);
}

Reflect is used because Reflect methods are 1-to-1 mirrors of the internal operations.

  • They produce correct return values (true/false/descriptor)
  • They avoid throwing errors that would break Proxy rules
  • They make the Proxy behave like normal JavaScript objects (unless modified)

Note

This is why every Proxy trap has a Reflect method with the same name and signature.


JavaScript Internals Became Reflect

Before ES6 (2015), many fundamental operations did not exist as functions:

Operation Before E6Problem
get propertyobj[prop]Not callable as a function
set propertyobj[prop] = valueNot callable as a function
delete propertydelete obj[prop]Operator, not a function
check property"prop" in objOperator, not a function
construct (new)new Foo()Not callable generically
get prototypesObject.getPrototypeOf(obj)Throws on non-objects
define propertyObject.defineProperty()Returns object instead of boolean
own keysObject.keys()Not complete (misses symbols)

These were incomplete and inconsistent for Proxy forwarding.

So ES6 (2015) added Reflect:

Internal Operation Reflect Method
[[Construct]]Reflect.construct()
[[Call]]Reflect.apply()
[[Get]]Reflect.get()
[[Set]]Reflect.set()
[[HasProperty]]Reflect.has()
[[Delete]]Reflect.deleteProperty()
[[DefineProperty]]Reflect.defineProperty()
[[GetOwnProperty]]Reflect.getOwnProperty()
[[OwnPropertyKeys]]Reflect.ownPropertyKeys()
[[IsExstensible]]Reflect.isExtensible()
[[PreventExstensions]]Reflect.preventExtensions()
[[GetPrototypeOf]]Reflect.getPrototypeOf()
[[SetPrototypeOf]]Reflect.setPrototypeOf()


×

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, cookies and privacy policy.

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