handler.has(target, property) is a trap that runs when you use the in operator on a Proxy, for example "name" in proxy. It is also used by Reflect.has(). The trap returns true or false.
Inside the trap you can hide properties, pretend that other properties exist, or build things like a number range. To answer in the normal way, use Reflect.has(target, property).
Remember
const handler = {
has(target, property) { ... }
};
target → the original object
property → the name (a string or a symbol)
return true → "property in proxy" is true
return false → "property in proxy" is false
Syntax
Add a has trap to the handler of a Proxy.
JavaScript
const user = new Proxy({ name: "Ann", _token: "abc" }, {
has(target, key) {
if (typeof key === "string" && key.startsWith("_")) {
return false; // hide private names
}
return Reflect.has(target, key);
}
});
console.log("name" in user); // true
console.log("_token" in user); // false
Call
What it does
"a" in proxy
The in operator runs the trap.
Reflect.has(proxy, "a")
The function-style version.
"a" in Object.create(proxy)
An object that inherits from the Proxy also runs the trap when the property is not found on it.
proxy.hasOwnProperty("a")
Does not run this trap. It uses getOwnPropertyDescriptor.
name in view: true
_token in view: false
Reflect.has: false
Read _token: abc
Own check: true
How It Works
Hide by name. For names that start with "_", the trap returns false. Reflect.has() runs the same trap, so it also says false.
Others are normal.name goes to Reflect.has(target, key) and is found.
Only in is affected. Reading view._token still works, and hasOwnProperty() still says true, because they use other traps.
Example 2 — Make a Number Range with in
A Proxy of an empty object can still answer the in operator. This one says that every whole number from 1 to 10 is "in" it.
JavaScript
const range = new Proxy({}, {
has(target, key) {
if (typeof key !== "string") {
return false;
}
const n = Number(key);
return Number.isInteger(n) && n >= 1 && n <= 10;
}
});
console.log("5 in range: " + (5 in range));
console.log("10 in range: " + (10 in range));
console.log("11 in range: " + (11 in range));
console.log("0 in range: " + (0 in range));
console.log("2.5 in range: " + (2.5 in range));
console.log("abc in range: " + ("abc" in range));
5 in range: true
10 in range: true
11 in range: false
0 in range: false
2.5 in range: false
abc in range: false
How It Works
Keys are strings. The number 5 arrives as "5". The trap turns it back with Number(key).
No real data. The target is an empty object. The trap makes up all the answers, which is allowed because the target has no locked properties.
Rules for the answer. Only whole numbers from 1 to 10 give true. 2.5 and "abc" are not whole numbers, so they give false.
Example 3 — Count the Checks and Follow the Rules
Count how many times the trap runs, and see which other operations do not run it. Then see that a trap cannot hide a frozen property. The catch block prints the error type only.
JavaScript
let checks = 0;
const counted = new Proxy({ a: 1 }, {
has(target, key) {
checks++;
return Reflect.has(target, key);
}
});
"a" in counted;
"b" in counted;
Reflect.has(counted, "a");
console.log("Checks: " + checks);
counted.hasOwnProperty("a");
Object.keys(counted);
console.log("After hasOwnProperty and Object.keys: " + checks);
const frozen = Object.freeze({ x: 1 });
const liar = new Proxy(frozen, {
has() {
return false;
}
});
try {
"x" in liar;
} catch (err) {
console.log("Hiding a frozen property: " + err.name);
}
Checks: 3
After hasOwnProperty and Object.keys: 3
Hiding a frozen property: TypeError
How It Works
Three checks. Two uses of the in operator and one Reflect.has() call run the trap, so the counter is 3.
Other operations skip it.hasOwnProperty() and Object.keys() use different traps, so the counter does not move.
No lying about frozen.x is non-configurable and cannot disappear. Answering false makes JavaScript throw a TypeError.
Compatibility
Browser & Runtime Support
handler.has() 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.has()
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.has()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 the in operator or Reflect.has() is used on a Proxy. It receives the target and the property name and returns true or false.
No. Those use the getOwnPropertyDescriptor and ownKeys traps. The has trap is only for the in operator, Reflect.has() and the with statement.
No. proxy.name still returns the value, because reading uses the get trap. To hide a property fully, add traps for get, ownKeys and getOwnPropertyDescriptor too.
Property names in JavaScript are always strings or symbols. 5 in proxy becomes the string "5", so convert it with Number(key) if you need a number.
Yes. Because the answer is made by your code, the target can be empty, as in the number range example. This is a fun way to give the in operator a custom meaning.