mozilla

Revision 434085 of Object.prototype.hasOwnProperty()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
  • Revision title: hasOwnProperty
  • Revision id: 434085
  • Created:
  • Creator: Nick_Pershin
  • Is current revision? No
  • Comment

Revision Content

Summary

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

Method of Object
Implemented in JavaScript ?
ECMAScript Edition ECMAScript 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

Example: Iterating over the properties of an object

The following example shows how to iterate over the properties of an object without executing on inherit properties. Note that the for..in loop is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that hasOwnProperty itself is confined strictly to enumerable items (as with getOwnPropertyNames).

var buz = {
    fog: 'stack'
};

for (var name in buz) {
    if (buz.hasOwnProperty(name)) {
        alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
    }
    else {
        alert(name); // toString or something else
    }
}

hasOwnProperty as a property

JavaScript does not protect the property name hasOwnProperty; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an external hasOwnProperty to get correct results:

var foo = {
    hasOwnProperty: function() {
        return false;
    },
    bar: 'Here be dragons'
};

foo.hasOwnProperty('bar'); // always returns false

// Use another Object's hasOwnProperty and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, 'bar'); // true

// It's also possible to use the hasOwnProperty property from the Object property for this purpose
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true

Note that in the last case there are no newly created objects.

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a boolean indicating whether the object has the specified property.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript ?</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 3rd Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>obj</em>.hasOwnProperty(<em>prop</em>)</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>prop</code></dt>
  <dd>
    The name of the property to test.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<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-US/docs/JavaScript/Reference/Operators/Special/in" title="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>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_hasOwnProperty_to_test_for_a_property.27s_existence" name="Example:_Using_hasOwnProperty_to_test_for_a_property.27s_existence">Example: Using <code>hasOwnProperty</code> to test for a property's existence</h3>
<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>
<h3 id="Example:_Direct_versus_inherited_properties" name="Example:_Direct_versus_inherited_properties">Example: Direct versus inherited properties</h3>
<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 id="Example:_Itarate_over_properties_not_considering_inherited_properties" name="Example:_Itarate_over_properties_not_considering_inherited_properties">Example: Iterating over the properties of an object</h3>
<p>The following example shows how to iterate over the properties of an object without executing on inherit properties. Note that the for..in loop is already only iterating enumerable items, so one should not assume based on the lack of non-enumerable properties shown in the loop that hasOwnProperty itself is confined strictly to enumerable items (as with <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a>).</p>
<pre class="brush: js">
var buz = {
    fog: 'stack'
};

for (var name in buz) {
    if (buz.hasOwnProperty(name)) {
        alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
    }
    else {
        alert(name); // toString or something else
    }
}</pre>
<h3 id="hasOwnProperty_as_a_property"><code>hasOwnProperty</code> as a property</h3>
<p>JavaScript does not protect the property name <code>hasOwnProperty</code>; thus, if the possibility exists that an object might have a property with this name, it is necessary to use an <em>external</em> <code>hasOwnProperty</code> to get correct results:</p>
<pre class="brush: js">
<code><span class="brush: js">var</span><span class="pln"> foo </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span>
<span class="pln">&nbsp; &nbsp; hasOwnProperty</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span>
<span class="pln">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span>
<span class="pln">&nbsp; &nbsp; </span><span class="pun">},</span>
<span class="pln">&nbsp; &nbsp; bar</span><span class="pun">:</span><span class="pln"> </span><span class="str">'</span></code><code><span class="str">Here be dragons</span></code><code><span class="str">'</span>
<span class="pun">};</span>

<span class="pln">foo</span><span class="pun">.</span><span class="pln">hasOwnProperty</span><span class="pun">(</span><span class="str">'bar'</span><span class="pun">);</span><span class="pln"> </span><span class="com">// always returns false</span>

<span class="com">// </span></code><code><span class="com">Use another Object's hasOwnProperty and call it with 'this' set to foo</span></code><code>
<span class="pun">({}).</span><span class="pln">hasOwnProperty</span><span class="pun">.</span><span class="pln">call</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">,</span><span class="pln"> </span><span class="str">'bar'</span><span class="pun">);</span><span class="pln"> </span><span class="com">// true</span>

<span class="com">// </span></code><code><span class="com">It's also possible to use the hasOwnProperty property from the Object property for this purpose</span></code><code>
<span class="typ">Object</span><span class="pun">.</span><span class="pln">prototype</span><span class="pun">.</span><span class="pln">hasOwnProperty</span><span class="pun">.</span><span class="pln">call</span><span class="pun">(</span><span class="pln">foo</span><span class="pun">,</span><span class="pln"> </span><span class="str">'bar'</span><span class="pun">);</span><span class="pln"> </span><span class="com">// true</span>
</code></pre>
<p>Note that in the last case there are no newly created objects.</p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties" title="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerability and ownership of properties</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for...in" title="JavaScript/Reference/Statements/for...in">for...in</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Operators/Special/in" title="Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator">in</a></li>
  <li><a href="/en-US/docs/JavaScript/Guide/Inheritance_Revisited" title="JavaScript/Guide/Inheritance_Revisited">inheritance</a></li>
</ul>
Revert to this revision