handler.get(target, property, receiver) is a trap that runs when you read a property of a Proxy, for example proxy.name or proxy["name"]. It is the most used trap, and a good one to learn first.
Inside the trap you decide what value the reader gets. You can return the real value, a default value, a calculated value or even throw an error. To read the real value in the normal way, use Reflect.get(target, property, receiver).
Remember
const handler = {
get(target, property, receiver) { ... }
};
target → the original object
property → the name (a string or a symbol)
receiver → the proxy (or an object that inherits from it)
return value → the value the reader gets
Syntax
Add a get trap to the handler of a Proxy.
JavaScript
const user = new Proxy({ name: "Ann" }, {
get(target, key, receiver) {
if (key in target) {
return Reflect.get(target, key, receiver);
}
return "unknown";
}
});
console.log(user.name); // Ann
console.log(user.age); // unknown
Call
What it does
proxy.name
Reading with a dot runs the trap.
proxy["name"], proxy[0]
Reading with square brackets runs it too. Numbers arrive as strings.
Reflect.get(proxy, "name")
The function-style version.
Object.create(proxy).name
An object that inherits from the Proxy also runs the trap when the property is not found on it.
// ECMAScript 2015 (ES6)
handler.get(target, property, receiver)
// target : the original object
// property : a string or a symbol
// receiver : the proxy, or an object that inherits from it
// returns : any value
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
property
The name of the property that is being read: a string or a symbol. A number such as list[0] arrives as the string "0".
receiver
The object that was used for the read. It is normally the Proxy itself. Pass it on to Reflect.get() so that getters use the right this.
Return Value
Return Value
Description
Any value
Whatever the trap returns is what the reader gets.
TypeError (thrown)
The trap broke a rule, for example by returning a different value for a read-only, non-configurable property of the target.
JavaScript Examples
Example 1 — Give a Default Value for Missing Properties
Normally a missing property gives undefined. With a get trap, you can return something more helpful.
JavaScript
const settings = new Proxy({ theme: "dark" }, {
get(target, key, receiver) {
if (key in target) {
return Reflect.get(target, key, receiver);
}
return "n/a";
}
});
console.log(settings.theme);
console.log(settings.language);
console.log("Has language: " + ("language" in settings));
Last: c
Second last: b
First: a
Length: 3
Joined: abc
How It Works
Keys are strings. The number -1 arrives as the string "-1". Number(key) turns it back, and typeof key === "string" keeps symbols away from Number().
Count from the end. For a negative whole number, the trap returns target[target.length + index], so -1 means the last item.
Everything else is normal.list[0], length and join() go through Reflect.get() and work as before.
Example 3 — Count Reads and Follow the Rules
Count how often each property is read. Then see that a trap cannot return a wrong value for a frozen property. The catch block prints the error type only.
Reads: {"a":2,"b":1}
Wrong value for frozen property: TypeError
How It Works
Count by name. Each read adds one to counts[key]. The trap then forwards the read, so the Proxy still works as normal.
Two reads of a.a was read twice and b once, which the JSON text shows.
A frozen value cannot change.pi is read-only and non-configurable. A trap that returns 3 instead of 3.14 is caught by JavaScript and throws a TypeError.
Compatibility
Browser & Runtime Support
handler.get() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.get()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.get()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when a property of a Proxy is read. It receives the target, the property name and the receiver, and whatever it returns is the value that the reader gets.
Use Reflect.get(target, property, receiver). It reads the property from the target in the normal way, including getters and inherited properties, and you can return the result.
JavaScript itself reads properties such as length, then, toString and Symbol.iterator, for example when you log an object, use await, or loop over it. Always forward names you do not handle with Reflect.get().
It is the object that the property was read from. Normally it is the Proxy. If an object inherits from the Proxy, it is that object. Pass it to Reflect.get() so that getters get the right this value.
Almost anything. The only limit is that for a read-only, non-configurable data property of the target, the trap must return the real value. Otherwise JavaScript throws a TypeError.