JavaScript Proxy Methods

About This Index
This page is the JavaScript Proxy Methods index. A Proxy has one static method, Proxy.revocable(), and thirteen traps: methods that you write in the handler object to catch actions on the Proxy. Each row below links to a full guide with syntax, examples you can run, and FAQs.
Learn what a Proxy is on /js/proxy. Use this page when you need a specific method such as handler.get() or handler.set().
You never call a trap such as handler.get() yourself. You put it in the handler object, like new Proxy(target, { get() { ... } }), and JavaScript calls it when someone reads a property of the Proxy. Only Proxy.revocable() is called directly.
How to Call a Proxy Method
Put the traps in a handler object, then create the Proxy:
const user = { name: "Ann", _token: "abc" };
const handler = {
get(target, key, receiver) {
return key in target ? Reflect.get(target, key, receiver) : "n/a";
},
has(target, key) {
return !String(key).startsWith("_") && Reflect.has(target, key);
}
};
const proxy = new Proxy(user, handler);
console.log(proxy.name); // Ann
console.log(proxy.age); // n/a
console.log("_token" in proxy); // false
const { proxy: temp, revoke } = Proxy.revocable(user, {});
revoke(); // temp can not be used any more JavaScript Proxy Methods Table
All method guides in this JavaScript tutorial series (14 tutorials), grouped by what they do.
Create a Proxy you can switch off
1 methodA static method of Proxy, called directly.
| Method | Description | Tutorial |
|---|---|---|
Proxy.revocable() | Create a Proxy together with a revoke() function that switches it off for good. | Open |
Read, write and check properties
4 methodsThe traps that are used the most.
| Method | Description | Tutorial |
|---|---|---|
handler.get() | Runs when a property is read. Give default values or add made-up properties. | Open |
handler.set() | Runs when a value is assigned. The best place to validate data. | Open |
handler.has() | Runs for the in operator. Hide names or build a custom check. | Open |
handler.deleteProperty() | Runs for the delete operator. Protect properties from removal. | Open |
Define and list properties
3 methodsThe traps behind descriptors, Object.keys() and for...in.
| Method | Description | Tutorial |
|---|---|---|
handler.defineProperty() | Runs when a property is defined, with Object.defineProperty() or sometimes with =. | Open |
handler.getOwnPropertyDescriptor() | Runs when a property descriptor is read. Hide properties from Object.keys() and JSON. | Open |
handler.ownKeys() | Runs when the keys are listed. Remove keys from Object.keys() and for...in. | Open |
Call functions and create objects
2 methodsThese two traps need a function or a class as the target.
| Method | Description | Tutorial |
|---|---|---|
handler.apply() | Runs when the Proxy of a function is called. Log, change or block the call. | Open |
handler.construct() | Runs when new is used on the Proxy of a class. It must return an object. | Open |
Work with prototypes
2 methodsRead and change what an object inherits from.
| Method | Description | Tutorial |
|---|---|---|
handler.getPrototypeOf() | Runs when the prototype is read, including by instanceof. | Open |
handler.setPrototypeOf() | Runs when the prototype is changed. Block or limit the change. | Open |
Lock an object
2 methodsThe traps for objects that cannot grow any more. Their answers are checked against the target.
| Method | Description | Tutorial |
|---|---|---|
handler.isExtensible() | Runs when JavaScript asks if new properties can be added. The answer must match the target. | Open |
handler.preventExtensions() | Runs when someone locks the object. It must really lock the target before it returns true. | Open |
Frequently Asked Questions
Start with handler.get()
Open the guide for catching property reads: syntax, examples, and FAQs.
14 people found this page helpful
