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 984197 of handler.has()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Proxy/handler/has
  • Revision title: handler.has()
  • Revision id: 984197
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment Remove {{harmony}}; remove "Experimental" tag; add link to ES draft spec

Revision Content

{{JSRef}}

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

Syntax

var p = 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.

Interceptions

This trap can intercept these operations:

  • Property query: foo in proxy
  • Inherited property query: foo in Object.create(proxy)
  • with check: with(proxy) { (foo); }
  • {{jsxref("Reflect.has()")}}

Invariants

If the following invariants are violated, the proxy will throw a {{jsxref("TypeError")}}:

  • 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 p = new Proxy({}, {
  has: function(target, prop) {
    console.log("called: " + prop);
    return true;
  }
});

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

The following code violates an invariant.

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

"a" in p; // TypeError is thrown

Specifications

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

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
  • {{jsxref("Reflect.has()")}}

Revision Source

<div>{{JSRef}}</div>

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

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js">
var p = new Proxy(target, {
  has: function(target, prop) {
  }
});
</pre>

<h3 id="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">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="Interceptions">Interceptions</h3>

<p>This trap can intercept these operations:</p>

<ul>
 <li>Property query: <code>foo in proxy</code></li>
 <li>Inherited property query: <code>foo in Object.create(proxy)</code></li>
 <li><code>with</code> check<code>: with(proxy) { (foo); }</code></li>
 <li>{{jsxref("Reflect.has()")}}</li>
</ul>

<h3 id="Invariants">Invariants</h3>

<p>If the following invariants are violated, the proxy will throw a {{jsxref("TypeError")}}:</p>

<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 p = new Proxy({}, {
  has: function(target, prop) {
    console.log("called: " + prop);
    return true;
  }
});

console.log("a" in p); // "called: a"
                       // true
</pre>

<p>The following code violates an invariant.</p>

<pre class="brush: js">
var obj = { a: 10 };
Object.preventExtensions(obj);
var p = new Proxy(obj, {
  has: function(target, prop) {
    return false;
  }
});

"a" in p; // TypeError is thrown
</pre>

<h2 id="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>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p', '[[HasProperty]]')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>

<h2 id="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">See also</h2>

<ul>
 <li>{{jsxref("Proxy")}}</li>
 <li>{{jsxref("Proxy.handler", "handler")}}</li>
 <li>{{jsxref("Operators/in", "in")}} operator</li>
 <li>{{jsxref("Reflect.has()")}}</li>
</ul>
Revert to this revision