JavaScript handler.has() Method

The method handler.has is utilized to "conceal" any property desired. It acts as a trap for an operator. This method returns a Boolean value of true if access to the property is permitted; otherwise, it returns false based on whether the key exists within the original object or not.

Syntax

Example

has: function(target, prop)

Parameters

target : The target object.

prop : The property to check for existence.

Return value

Return a Boolean value.

Browser Support

Chrome 49
Edge 12
Firefox 18
Opera 36

Example 1

Example

const x = { f:10 };

const proxy = new Proxy(x, {

  has: function(target, name) {

   document.writeln('in has');

     //expected output: in has

 return name in target;

  }

});

document.writeln('f' in proxy);

//expected output: true

Output:

Output

in has true

Example 2

Example

var x={hoo:1}

var xyz = new Proxy(x,  {

  has: function(target, key) {

    if(key == "a") return false;

      return true;

    }

  }

)

var x= ("a" in xyz)	

var y = ("b" in xyz)	

document.write("a in d: " +x)

//expected output: a in d: false

document.write('<br/>');

document.write("b in d: " +y) 

//expected output: b in d: true

Output:

Output

a in d: false

b in d: true

Example 3

Example

var s={

  voo:1

}

var p = new Proxy(s, {

  has: function(target, prop) {

    document.writeln('called: ' + prop);

    return false;

  }

});

document.writeln('a' in p); 

//expected output:called: a false

Output:

Output

called: a false

Input Required

This code uses input(). Please provide values below: