JS_HasInstance

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_HasInstance
  • Revision title: JS_HasInstance
  • Revision id: 326425
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

JSAPI method equivalent to the instanceof operator in javascript.

Syntax

JSBool JS_HasInstance(JSContext *cx, JSObject *obj, jsval *v, JSBool *bp);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}
obj JSObject * Constructor/prototype to test.
v jsval *

The value to test

bp JSBool * Out parameter. On success, *bp receives the result of the test.

Description

JS_HasInstance determines if a specified JS value, v, is an instance of JS object, obj. This function is equivalent to v instanceof obj test in javascript.  On success, JS_HasInstance stores the result of the instanceof test in *bp and returns JS_TRUE. On error or exception, it returns JS_FALSE, and the value left in *bp is undefined.

When providing a prototype as obj, the prototype's JSClass must have its hasInstance method set. Otherwise JS_HasInstance returns JS_FALSE.

{{ LXRSearch("ident", "i", "JS_HasInstance") }}

Revision Source

<p>JSAPI method equivalent to the instanceof operator in javascript.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval">
<a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_HasInstance</strong>(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/JSObject">JSObject</a> *obj, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *v, <a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> *bp);</pre>
<table class="fullwidth-table">
  <tbody>
    <tr>
      <th>Name</th>
      <th>Type</th>
      <th>Description</th>
    </tr>
    <tr>
      <td><code>cx</code></td>
      <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a>&nbsp;*</code></td>
      <td>Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}</td>
    </tr>
    <tr>
      <td><code>obj</code></td>
      <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/JSObject">JSObject</a> *</code></td>
      <td>Constructor/prototype to test.</td>
    </tr>
    <tr>
      <td><code>v</code></td>
      <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *</code></td>
      <td>
        <p>The value to test</p>
      </td>
    </tr>
    <tr>
      <td><code>bp</code></td>
      <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> *</code></td>
      <td>Out parameter. On success,&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">*bp</code>&nbsp;receives the result of the test.</td>
    </tr>
  </tbody>
</table>
<h2 id="Description" name="Description">Description</h2>
<p><code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_HasInstance</code>&nbsp;determines if a specified JS value, <code><code>v</code></code>, is an instance of&nbsp;JS object,&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">obj</code>. This function is equivalent to <code>v instanceof obj</code>&nbsp;test in javascript.&nbsp;&nbsp;On success,&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_HasInstance</code>&nbsp;stores the result of the instanceof test in&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">*bp</code>&nbsp;and returns&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_TRUE</code>. On error or exception, it returns&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_FALSE</code>, and the value left in&nbsp;<code style="color: rgb(37, 34, 29); font-weight: inherit; ">*bp</code>&nbsp;is undefined.</p>
<p>When providing a prototype as <code style="color: rgb(37, 34, 29); font-weight: inherit; ">obj</code>, the prototype's <code><a href="/En/SpiderMonkey/JSAPI_Reference/JSClass" title="En/SpiderMonkey/JSAPI_Reference/JSClass">JSClass </a></code>must have its <code><a href="/En/SpiderMonkey/JSAPI_Reference/JSClass.hasInstance" title="En/SpiderMonkey/JSAPI_Reference/JSClass.hasInstance">hasInstance </a></code> method set. Otherwise <code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_HasInstance</code> returns <code style="color: rgb(37, 34, 29); font-weight: inherit; ">JS_FALSE.</code></p>
<p>{{ LXRSearch("ident", "i", "JS_HasInstance") }}</p>
Revert to this revision