The has(target, property) method

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

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.