Skip to content
Related Articles

Related Articles

JavaScript | handler.has() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Sep, 2021

The handler.has() method in JavaScript is used to “hide” any property that you want. It is a trap for an operator. It returns the Boolean value.
Syntax: 
 

const p = new Proxy(target, {
  has: function(target, prop) {
  }
});

Parameters: This method accept two parameters as mentioned above and described below: 
 

  • target: This parameter is the target object.
  • prop: This parameter is the property which is going to be check for existence.

Return value: This method return a Boolean value true if you want the property to be accessed.
Below examples illustrate the handler.has() method in JavaScript:
Example 1: 
 

javascript




<script>
const handler1 = {
  has (target, key) {
    if (key[1] === '1') {
      return false;
    }
    return key in target;
  }
};
 
const monster1 = {
  p1roperty1: 'GeeksforGeeks',
  property2: 4
};
 
const proxy1 = new Proxy(monster1, handler1);
console.log('property2' in proxy1);
console.log('p1roperty1' in proxy1);
console.log('p1roperty1' in monster1);
</script>

Output: 
 

true
false
true

Example 2: 
 

javascript




<script>
var s={ 
  value:1 
var p = new Proxy(s, { 
  has: function(target, prop) { 
    console.log( prop); 
    return false
  
}); 
console.log('prop' in p);
 
var p1 = new Proxy(s, { 
  has: function(target, prop) { 
    console.log( prop); 
    return true
  
}); 
console.log('prop' in p1);
</script>

Output: 
 

"prop"
false
"prop"
true

Supported Browsers: The browsers supported by handler.has() method are listed below: 
 

  • Google Chrome  49 and above
  • Edge 12 and above
  • Firefox 18 and above
  • Opera 36 and above
  • Safari 10 and above

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!