JS_GetProperty

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_GetProperty
  • Revision title: JS_GetProperty
  • Revision id: 131744
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 14 words removed

Revision Content

Finds a specified property and retrieves its value.

Syntax

JSBool JS_GetProperty(JSContext *cx, JSObject *obj,
    const char *name, jsval *vp);

JSBool JS_GetUCProperty(JSContext *cx, JSObject *obj,
    const jschar *name, size_t namelen, jsval *vp);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}
obj JSObject * Object to search on for the property.
name const char * or const jschar * Name of the property to look up.
namelen size_t (in JS_GetUCProperty only) The length of name, in characters; or -1 to indicate that name is null-terminated.
vp jsval * Out parameter. On success, *vp receives the current value of the property.

Description

JS_GetProperty examines a specified JS object obj and its prototype chain for a property with the specified name. JS_GetUCProperty is the Unicode version of the function.

If the property is found, JS_GetProperty sets *vp to the current value of the property and returns JS_TRUE.

If the object obj has no such property, *vp is set to JSVAL_VOID and JS_GetProperty returns JS_TRUE (to indicate that no error occurred).

If the search fails with an error or exception, JS_GetProperty returns JS_FALSE, and the value left in *vp is undefined.

See Also

{{ LXRSearch("ident", "i", "JS_GetProperty") }}
{{ LXRSearch("ident", "i", "JS_GetUCProperty") }}

Example in the JSAPI Phrasebook

JS_DefineProperty, JS_DefinePropertyWithTinyId, JS_DeleteProperty, JS_DeleteProperty2, JS_LookupProperty, JS_PropertyStub, JS_SetProperty

Revision Source

<p>Finds a specified property and retrieves its value.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_GetProperty</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,
    const char *name, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *vp);

<a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_GetUCProperty</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,
    const <a href="/en/SpiderMonkey/JSAPI_Reference/jschar" title="en/jschar">jschar</a> *name, size_t namelen, <a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *vp);
</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> *</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>Object to search on for the property.</td>
</tr>
<tr>
<td><code>name</code></td>
<td><code>const char *</code> <em>or</em> <code>const <a href="/en/SpiderMonkey/JSAPI_Reference/jschar" title="en/jschar">jschar</a> *</code></td>
<td>Name of the property to look up.</td>
</tr>
<tr>
<td><code>namelen</code></td>
<td><code>size_t</code></td>
<td><em>(in <code>JS_GetUCProperty</code> only)</em> The length of <code>name</code>, in characters; or <code>-1</code> to indicate that <code>name</code> is null-terminated.</td>
</tr>
<tr>
<td><code>vp</code></td>
<td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *</code></td>
<td>Out parameter. On success, <code>*vp</code> receives the current value of the property.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><strong><code>JS_GetProperty</code></strong> examines a specified JS object <code>obj</code> and its prototype chain for a property with the specified <code>name</code>. <strong><code>JS_GetUCProperty</code></strong> is the Unicode version of the function.
</p><p>If the property is found, <code>JS_GetProperty</code> sets <code>*vp</code> to the current value of the property and returns <code>JS_TRUE</code>.
</p><p>If the object <code>obj</code> has no such property, <code>*vp</code> is set to <code><a href="/en/SpiderMonkey/JSAPI_Reference/JSVAL_VOID" title="en/JSVAL_VOID">JSVAL_VOID</a></code> and <code>JS_GetProperty</code> returns <code>JS_TRUE</code> (to indicate that no error occurred).
</p><p>If the search fails with an error or exception, <code>JS_GetProperty</code> returns <code>JS_FALSE</code>, and the value left in <code>*vp</code> is undefined.
</p>
<h2 name="See_Also"> See Also </h2>
<p>{{ LXRSearch("ident", "i", "JS_GetProperty") }}<br>
{{ LXRSearch("ident", "i", "JS_GetUCProperty") }}
</p><p><a href="/En/SpiderMonkey/JSAPI_Phrasebook#Getting_a_property" title="en/JSAPI_Phrasebook#Getting_a_property">Example in the JSAPI Phrasebook</a>
</p><p><a href="/en/SpiderMonkey/JSAPI_Reference/JS_DefineProperty" title="en/JS_DefineProperty">JS_DefineProperty</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_DefinePropertyWithTinyId" title="en/JS_DefinePropertyWithTinyId">JS_DefinePropertyWithTinyId</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_DeleteProperty" title="en/JS_DeleteProperty">JS_DeleteProperty</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_DeleteProperty2" title="en/JS_DeleteProperty2">JS_DeleteProperty2</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_LookupProperty" title="en/JS_LookupProperty">JS_LookupProperty</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_PropertyStub" title="en/JS_PropertyStub">JS_PropertyStub</a>,
<a href="/en/SpiderMonkey/JSAPI_Reference/JS_SetProperty" title="en/JS_SetProperty">JS_SetProperty</a>
</p>
Revert to this revision