1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
---
title: JS_HasOwnProperty
slug: Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_HasOwnProperty
tags:
- 中文
translation_of: Mozilla/Projects/SpiderMonkey/JSAPI_reference/JS_HasOwnProperty
---
<div>{{SpiderMonkeySidebar("JSAPI")}}</div>
<p>{{ jsapi_minversion_header("45") }}</p>
<div class="summary">
<p>Determine whether a JavaScript object has a specified own property.</p>
</div>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="brush: cpp notranslate">bool
JS_HasOwnProperty(JSContext* cx, HandleObject obj, const char* name,
bool* foundp)
bool
JS_HasOwnPropertyById(JSContext* cx, HandleObject obj, HandleId id,
bool* foundp)
</pre>
<table class="fullwidth-table">
<tbody>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td><code>cx</code></td>
<td>{{jsapixref("JSRuntime", "JSContext *")}}</td>
<td>A context. {{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td>{{jsapixref("JSObject", "JS::HandleObject")}}</td>
<td>Object to search on for the property.</td>
</tr>
<tr>
<td><code>name</code> <em>or<em> <code>id</code></em></em></td>
<td><code>const char *</code> <em>or</em> {{jsapixref("jsid", "JS::HandleId")}}</td>
<td>Name of the property to look up.</td>
</tr>
<tr>
<td><code>foundp</code></td>
<td><code>bool *</code></td>
<td>Non-null pointer to a variable of type <code>bool</code>. On success, <code>JS_HasOwnProperty</code> stores <code>true</code> in this variable if <code>obj</code> has an own property with the given <code>name</code>, and <code>false</code> if not.</td>
</tr>
</tbody>
</table>
<h2 id="Description" name="Description">Description</h2>
<p><strong><code>JS_HasOwnProperty</code></strong> searches an object, <code>obj</code>, for an own property with the specified <code>name</code>. It behaves like the JavaScript expression <code>Object.hasOwnProperty(obj, name)</code>. <strong><code>JS_HasOwnPropertyById</code></strong> is the same but takes a {{jsapixref("jsid", "JS::HandleId")}} for the property name.</p>
<p>If the property exists, this function sets <code>*foundp</code> to <code>true</code> and returns <code>true</code>.</p>
<p>If the object <code>obj</code> has no such property, the function sets <code>*foundp</code> to <code>false</code> and returns <code>true</code> (to indicate that no error occurred).</p>
<p>If an error occurs during the search, the function returns <code>false</code>, and the value of <code>*foundp</code> is undefined.</p>
<h2 id="See_Also" name="See_Also">See Also</h2>
<ul>
<li>{{ LXRSearch("ident", "i", "JS_HasOwnProperty") }}</li>
<li>{{ LXRSearch("ident", "i", "JS_HasOwnPropertyById") }}</li>
<li>{{bug(1163423)}}</li>
</ul>
|