JS_SetPrototype

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_SetPrototype
  • Revision title: JS_SetPrototype
  • Revision id: 146717
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment no wording changes; page display name changed to 'JS_SetPrototype'

Revision Content

{{ Jsapi_ref_header("JS_SetPrototype") }}

Set a JavaScript object's prototype.

Syntax

JSBool JS_SetPrototype(JSContext *cx, JSObject *obj, JSObject *proto);
Name Type Description
cx JSContext * The context in which to set the object's prototype. {{ Jsapi-requires-request() }}
obj JSObject * The object to modify.
proto JSObject * The object to set as the new prototype of obj.

Description

JS_SetPrototype sets the prototype object for a specified object. A prototype object provides properties that are shared by similar JS object instances. Ordinarily you set a prototype for an object when you create the object with JS_NewObject, but if you do not set a prototype at that time, you can later call JS_SetPrototype to do so.

obj is a pointer to an existing JS object, and proto is a pointer to second existing object upon which the first object is to be based.

On success, JS_SetPrototype returns JS_TRUE. Otherwise it returns JS_FALSE.

Take care not to create a circularly-linked list of prototypes using this function, because such a set of prototypes cannot be resolved by the JavaScript engine and can easily lead to an infinite loop.

To get an object's prototype, use JS_GetPrototype.

{{ LXRSearch("ident", "i", "JS_SetPrototype", "LXR ID Search") }}

Revision Source

<p>{{ Jsapi_ref_header("JS_SetPrototype") }}
</p><p>Set a JavaScript object's prototype.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="/en/JSBool" title="en/JSBool">JSBool</a> <strong>JS_SetPrototype</strong>(<a href="/en/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/en/JSObject" title="en/JSObject">JSObject</a> *obj, <a href="/en/JSObject" title="en/JSObject">JSObject</a> *proto);
</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/JSRuntime" title="en/JSRuntime">JSContext</a> *</code></td>
<td>The context in which to set the object's prototype.
{{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td><code><a href="/en/JSObject" title="en/JSObject">JSObject</a> *</code></td>
<td>The object to modify.</td>
</tr>
<tr>
<td><code>proto</code></td>
<td><code><a href="/en/JSObject" title="en/JSObject">JSObject</a> *</code></td>
<td>The object to set as the new prototype of <code>obj</code>.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><strong><code>JS_SetPrototype</code></strong> sets the prototype object for a specified object. A prototype object provides properties that are shared by similar JS object instances. Ordinarily you set a prototype for an object when you create the object with <code><a href="/en/JS_NewObject" title="en/JS_NewObject">JS_NewObject</a></code>, but if you do not set a prototype at that time, you can later call <code>JS_SetPrototype</code> to do so.
</p><p><code>obj</code> is a pointer to an existing JS object, and <code>proto</code> is a pointer to second existing object upon which the first object is to be based.
</p><p>On success, <code>JS_SetPrototype</code> returns <code>JS_TRUE</code>. Otherwise it returns <code>JS_FALSE</code>.
</p><p>Take care not to create a circularly-linked list of prototypes using this function, because such a set of prototypes cannot be resolved by the JavaScript engine and can easily lead to an infinite loop.
</p><p>To get an object's prototype, use <code><a href="/en/JS_GetPrototype" title="en/JS_GetPrototype">JS_GetPrototype</a></code>.
</p><p>{{ LXRSearch("ident", "i", "JS_SetPrototype", "LXR ID Search") }}
</p>
Revert to this revision