The has trap is executed when we check if a property exists or not, using the in operator. It takes two parameters--that is, the target object and the property name. It must return a Boolean value that indicates whether the property exists or not.
Here is a code example that demonstrates how to use the has trap:
const proxy = new Proxy({age: 12}, { has(target, property) { return property in target;}}); console.log(Reflect.has(proxy, "name")); console.log(Reflect.has(proxy, "age"));
The output is as follows:
falsetrue