- Understand the Proxy structural pattern
- Use JavaScript's Proxy API
- Implement validation, logging, and caching proxies
- Control access to objects
A Proxy wraps an object and intercepts operations on it. This enables:
- Validation before setting properties
- Logging property access
- Lazy loading
- Access control
- Caching
// Without proxy: no control
user.age = -5; // Invalid but allowed!
// With proxy: validation
const safeUser = createValidatingProxy(user, {
age: (value) => value >= 0 && value <= 150,
});
safeUser.age = -5; // Throws error!Create a proxy that validates property values:
const user = createValidatingProxy(
{},
{
name: (v) => typeof v === "string" && v.length > 0,
age: (v) => Number.isInteger(v) && v >= 0,
email: (v) => /^[^@]+@[^@]+\.[^@]+$/.test(v),
},
);
user.name = "Alice"; // OK
user.age = 25; // OK
user.age = -5; // Error!
user.email = "bad"; // Error!Create a proxy that logs all property access:
const logged = createLoggingProxy(obj, (action, prop, value) => {
console.log(`${action} ${prop}: ${value}`);
});
logged.name = "Alice"; // Log: "set name: Alice"
logged.name; // Log: "get name: Alice"
delete logged.name; // Log: "delete name: undefined"Create a proxy that caches method results:
const api = {
fetchUser(id) {
/* expensive */
},
};
const cached = createCachingProxy(api, ["fetchUser"]);
cached.fetchUser(1); // Fetches
cached.fetchUser(1); // Returns cachedCreate a proxy that restricts access:
const admin = { role: "admin", secret: "12345" };
const restricted = createAccessProxy(admin, {
readable: ["role"],
writable: [],
});
restricted.role; // 'admin'
restricted.secret; // Error: Access denied
restricted.role = "user"; // Error: Property is read-only// JavaScript Proxy basics
const handler = {
get(target, prop) {
console.log(`Getting ${prop}`);
return target[prop];
},
set(target, prop, value) {
console.log(`Setting ${prop} = ${value}`);
target[prop] = value;
return true;
},
};
const proxy = new Proxy({}, handler);
proxy.name = "Alice"; // "Setting name = Alice"
proxy.name; // "Getting name" -> "Alice"
// Traps available: get, set, has, deleteProperty, apply, construct, etc.- Use JavaScript's built-in
Proxyclass - Handler traps:
get,set,has,deleteProperty, etc. - Return
truefromsettrap to indicate success - Use
Reflectfor default behavior:Reflect.get(target, prop) - For methods, check if value is function before caching