TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript handler has() Method

JavaScript handler has() Method with examples on javascript handler, javascript methods, javascript tutorial, apply(), construct(), defineProperty(), deleteProperty(), get(), setPrototypeOf(), set(), ownKeys(), isExtensible() etc.

<< Back to JAVASCRIPT

JavaScript handler.has() Method

The handler.has() method used to "hide" any property you want. It's a trap for an operator. It returns the Boolean value true if you want the property to be accessed, otherwise false If the key is present or not in the original object.

Syntax

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

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
Test it Now

Output:

in has true

Example 2

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('
'); document.write("b in d: " +y) //expected output: b in d: true
Test it Now

Output:

a in d: false
b in d: true

Example 3

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
Test it Now

Output:

called: a false

Next TopicJavaScript handler




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf