Proxy.revocable
Creates a revocable Proxy object, returning both the proxy and a revoke function that disables the proxy
Syntax
Proxy.revocable(target, handler)Parameters
| Parameter | Type | Description |
|---|---|---|
| target | object | The target object to wrap |
| handler | ProxyHandler | An object with trap functions |
Return Value
An object with proxy and revoke properties
Examples
const target = { secret: 'data' }
const { proxy, revoke } = Proxy.revocable(target, {})
console.log(proxy.secret) // 'data'
revoke()
// proxy.secret // throws TypeErrorfunction createTemporaryAccess<T extends object>(obj: T, ttlMs: number) {
const { proxy, revoke } = Proxy.revocable(obj, {})
setTimeout(revoke, ttlMs)
return proxy
}const { proxy, revoke } = Proxy.revocable({ x: 1, y: 2 }, {
get(target, prop) {
console.log(`Reading ${String(prop)}`)
return Reflect.get(target, prop)
}
})
console.log(proxy.x)
revoke()Understanding Proxy.revocable
The Proxy.revocable method in JavaScript creates a revocable Proxy object, returning both the proxy and a revoke function that disables the proxy. It belongs to the Proxy object and is one of the most widely used methods for working with proxy values in modern JavaScript and TypeScript applications.
The method signature is Proxy.revocable(target, handler). It accepts 2 parameters: target, handler. When called, it returns an object with proxy and revoke properties. Understanding when and how to use revocable() helps you write more expressive, readable code.
Common use cases for Proxy.revocable include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like proxy-constructor, reflect-get, reflect-set, enabling you to chain operations together for complex data manipulation pipelines.
Supported in all modern browsers (Chrome 63+, Firefox 34+, Safari 10+, Edge 12+) and Node.js 6+.
Browser Compatibility
Supported in all modern browsers (Chrome 63+, Firefox 34+, Safari 10+, Edge 12+) and Node.js 6+.
Related Methods
ProxyCreates a new Proxy object that wraps a target object and intercepts operations defined by a handler
Reflect.getGets the value of a property on an object, similar to target[propertyKey] but as a function
Reflect.setSets the value of a property on an object, similar to target[propertyKey] = value but as a function
More Proxy Methods
Other methods in the Proxy object
Related Tools
More Proxy Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.