Object.prototype.hasOwnProperty()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/hasOwnProperty
  • Revision title: hasOwnProperty
  • Revision id: 81776
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[fr:Référence de JavaScript 1.5 Core:Objets globaux:Object:hasOwnProperty]] <<langbot>>

Revision Content

Summary

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

Syntax

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>
<h3 name="Syntax"> Syntax </h3>
<p><code>
hasOwnProperty(<i>prop</i>)
</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/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>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>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/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator">in</a>
</p>
<div class="noinclude">
</div>
{{ 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" } ) }}
Revert to this revision