Skip to content

StdLib Reflect Proxy

Roger Johansson edited this page Jan 14, 2026 · 1 revision

Reflect and Proxy

Metaprogramming utilities for intercepting and customizing object operations.


Reflect

A built-in object providing methods for interceptable JavaScript operations.

Implementation Status: 100% Complete

Method Status Description
Reflect.apply(target, thisArg, args) Implemented Calls function with args
Reflect.construct(target, args, newTarget) Implemented Calls constructor
Reflect.defineProperty(target, prop, desc) Implemented Defines property
Reflect.deleteProperty(target, prop) Implemented Deletes property
Reflect.get(target, prop, receiver) Implemented Gets property value
Reflect.getOwnPropertyDescriptor(target, prop) Implemented Gets descriptor
Reflect.getPrototypeOf(target) Implemented Gets prototype
Reflect.has(target, prop) Implemented Checks for property (in)
Reflect.isExtensible(target) Implemented Checks extensibility
Reflect.ownKeys(target) Implemented Gets all own keys
Reflect.preventExtensions(target) Implemented Prevents extensions
Reflect.set(target, prop, value, receiver) Implemented Sets property
Reflect.setPrototypeOf(target, proto) Implemented Sets prototype

Reflect vs Object Methods

Operation Reflect Object
Returns Boolean success Object or throws
Errors Returns false Throws TypeError
Use Case Proxy trap forwarding General use

Proxy

Creates a proxy that intercepts operations on a target object.

Implementation Status: 69% Complete (9/13 traps)

Constructor

Method Status Description
new Proxy(target, handler) Implemented Creates proxy
Proxy.revocable(target, handler) Implemented Creates revocable proxy

Supported Traps

Trap Status Intercepts
get(target, prop, receiver) Implemented Property read
set(target, prop, value, receiver) Implemented Property write
has(target, prop) Implemented in operator
deleteProperty(target, prop) Implemented delete operator
defineProperty(target, prop, desc) Implemented Property definition
getOwnPropertyDescriptor(target, prop) Implemented Descriptor lookup
ownKeys(target) Implemented Key enumeration
getPrototypeOf(target) Implemented Prototype access
setPrototypeOf(target, proto) Implemented Prototype modification

Not Implemented Traps

Trap Status Intercepts
apply(target, thisArg, args) Not Implemented Function call
construct(target, args, newTarget) Not Implemented new operator
isExtensible(target) Not Implemented Extensibility check
preventExtensions(target) Not Implemented Extension prevention

Revocable Proxy

const { proxy, revoke } = Proxy.revocable(target, handler);

proxy.foo;  // Works
revoke();
proxy.foo;  // TypeError: proxy revoked

Example Usage

Basic Proxy

const handler = {
    get(target, prop, receiver) {
        console.log(`Getting ${prop}`);
        return Reflect.get(target, prop, receiver);
    },
    set(target, prop, value, receiver) {
        console.log(`Setting ${prop} = ${value}`);
        return Reflect.set(target, prop, value, receiver);
    }
};

const proxy = new Proxy({}, handler);
proxy.x = 42;  // "Setting x = 42"
proxy.x;       // "Getting x"

Validation Proxy

const validator = {
    set(target, prop, value) {
        if (prop === 'age' && typeof value !== 'number') {
            throw new TypeError('Age must be a number');
        }
        return Reflect.set(target, prop, value);
    }
};

const person = new Proxy({}, validator);
person.age = 30;    // OK
person.age = "30";  // TypeError

Implementation Notes

Trap Forwarding

Proxy traps should forward to the target via Reflect methods:

// Correct - forwards to target
get(target, prop, receiver) {
    return Reflect.get(target, prop, receiver);
}

// Wrong - loses prototype chain
get(target, prop) {
    return target[prop];
}

Invariants

The proxy enforces certain invariants:

  • Non-configurable properties cannot be hidden
  • Non-extensible objects cannot gain properties
  • Sealed/frozen objects maintain their state

Files

Type Files
Reflect StdLib/Reflect/ReflectPrototype.cs, ReflectHelper.cs
Proxy StdLib/Proxy/ProxyConstructor.cs, JsTypes/JsProxy.cs

See Also

Clone this wiki locally