Revision 81778 of Object.prototype.hasOwnProperty()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/hasOwnProperty
  • Revision title: hasOwnProperty
  • Revision id: 81778
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 17 words added

Revision Content

 

Summary

Returns a boolean indicating whether the object has the specified property.

Method of Object
Implemented in: ?
ECMAScript Edition: ECMA-262 3rd Edition

Syntax

obj.hasOwnProperty(prop)

Parameters

prop
The name of the property to test.

Description

Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.

Examples

Example: Using hasOwnProperty to test for a property's existence

The following example determines whether the o object contains a property named prop:

o = new Object();
o.prop = 'exists';

function changeO() {
  o.newprop = o.prop;
  delete o.prop;
}

o.hasOwnProperty('prop');   //returns true
changeO();
o.hasOwnProperty('prop');   //returns false

Example: Direct versus inherited properties

The following example differentiates between direct properties and properties inherited through the prototype chain:

o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // returns true
o.hasOwnProperty('toString');         // returns false
o.hasOwnProperty('hasOwnProperty');   // returns false

See also

in

{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/hasOwnProperty", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty" } ) }}

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Returns a boolean indicating whether the object has the specified property.</p>
<table class="fullwidth-table"> <tbody> <tr> <td class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Object" title="en/JavaScript/Reference/Global_Objects/Object">Object</a></td> </tr> <tr> <td>Implemented in:</td> <td>?</td> </tr> <tr> <td>ECMAScript Edition:</td> <td>ECMA-262 3rd Edition</td> </tr> </tbody>
</table>
<h3 name="Syntax">Syntax</h3>
<p><code><em>obj</em>.hasOwnProperty(<em>prop</em>)</code></p>
<h3 name="Parameters">Parameters</h3>
<dl> <dt><code>prop</code></dt> <dd>The name of the property to test.</dd>
</dl>
<h3 name="Description">Description</h3>
<p>Every object descended from <code>Object</code> inherits the <code>hasOwnProperty</code> method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the <a href="/en/JavaScript/Reference/Operators/Special/in" title="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator"><code>in</code></a> operator, this method does not check down the object's prototype chain.</p>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_hasOwnProperty_to_test_for_a_property.27s_existence">Example: Using <code>hasOwnProperty</code> to test for a property's existence</h4>
<p>The following example determines whether the <code>o</code> object contains a property named <code>prop</code>:</p>
<pre class="brush: js">o = new Object();
o.prop = 'exists';

function changeO() {
  o.newprop = o.prop;
  delete o.prop;
}

o.hasOwnProperty('prop');   //returns true
changeO();
o.hasOwnProperty('prop');   //returns false
</pre>
<h4 name="Example:_Direct_versus_inherited_properties">Example: Direct versus inherited properties</h4>
<p>The following example differentiates between direct properties and properties inherited through the prototype chain:</p>
<pre class="brush: js">o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop');             // returns true
o.hasOwnProperty('toString');         // returns false
o.hasOwnProperty('hasOwnProperty');   // returns false
</pre>
<h3 name="See_also">See also</h3>
<p><a href="/en/JavaScript/Reference/Operators/Special/in" title="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator">in</a></p>
<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/hasOwnProperty", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/hasOwnProperty" } ) }}</p>
Revert to this revision