Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 719413 of handler.has()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/has
  • Revision title: handler.has()
  • Revision id: 719413
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment

Revision Content

{{JSRef("Global_Objects", "Proxy")}} {{harmony}}

Summary

The handler.has() method is a trap for the {{jsxref("Operators/in", "in")}} operator.

Syntax

var proxy = new Proxy(target, {
  has: function(target, prop) {
  }
});

Parameters

The following parameters are passed to has method. this is bound to the handler.

target
The target object.
prop
The name of the property to check for existence.

Return value

The has method must return a boolean value.

Description

The handler.has method is a trap for the {{jsxref("Operators/in", "in")}} operator.

Invariants

  • A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.
  • A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.

Examples

The following code traps the {{jsxref("Operators/in", "in")}} operator.

var proxy = new Proxy({}, {
  has: function(target, prop) {
    console.log("called: " + prop);
    return true;
  }
});

console.log("a" in proxy); // "called: a"
                           // true

The following code violates an invariant.

var obj = { a: 10 };
Object.preventExtensions(obj);
var proxy = new Proxy(obj, {
  has: function(target, prop) {
    return false;
  }
});

"a" in proxy; // TypeError is thrown

Specifications

Specification Status Comment
{{SpecName('ES6', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}} {{Spec2('ES6')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatUnknown}} {{CompatGeckoDesktop("18")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile("18")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

See also

  • {{jsxref("Proxy")}}
  • {{jsxref("Proxy.handler", "handler")}}
  • {{jsxref("Operators/in", "in")}} operator

Revision Source

<div>
 {{JSRef("Global_Objects", "Proxy")}} {{harmony}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <strong><code>handler.has()</code></strong> method is a trap for the {{jsxref("Operators/in", "in")}} operator.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="brush: js">
var proxy = new Proxy(target, {
  has: function(target, prop) {
  }
});
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>The following parameters are passed to <code>has</code> method. <code>this</code> is bound to the handler.</p>
<dl>
 <dt>
  <code>target</code></dt>
 <dd>
  The target object.</dd>
 <dt>
  <code>prop</code></dt>
 <dd>
  The name of the property to check for existence.</dd>
</dl>
<h3 id="Return_value" name="Return_value">Return value</h3>
<p>The <code>has</code> method must return a boolean value.</p>
<h2 id="Description">Description</h2>
<p>The <code><strong>handler.has</strong></code> method is a trap for the {{jsxref("Operators/in", "in")}} operator.</p>
<h3 id="Invariants">Invariants</h3>
<ul>
 <li>A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object.</li>
 <li>A property cannot be reported as non-existent, if it exists as an own property of the target object and the target object is not extensible.</li>
</ul>
<h2 id="Examples">Examples</h2>
<p>The following code traps the {{jsxref("Operators/in", "in")}} operator.</p>
<pre class="brush: js">
var proxy = new Proxy({}, {
  has: function(target, prop) {
    console.log("called: " + prop);
    return true;
  }
});

console.log("a" in proxy); // "called: a"
                           // true
</pre>
<p>The following code violates an invariant.</p>
<pre class="brush: js">
var obj = { a: 10 };
Object.preventExtensions(obj);
var proxy = new Proxy(obj, {
  has: function(target, prop) {
    return false;
  }
});

"a" in proxy; // TypeError is thrown
</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>
 {{CompatibilityTable}}</div>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatGeckoDesktop("18")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatGeckoMobile("18")}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
    <td>{{CompatUnknown}}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("Proxy")}}</li>
 <li>{{jsxref("Proxy.handler", "handler")}}</li>
 <li>{{jsxref("Operators/in", "in")}} operator</li>
</ul>
Revert to this revision