Enumerability and ownership of properties

  • Revision slug: Enumerability_and_ownership_of_properties
  • Revision title: Enumerability and ownership of properties
  • Revision id: 351941
  • Created:
  • Creator: manish.jethani
  • Is current revision? No
  • Comment

Revision Content

Enumerable properties are those which can be iterated by a for..in loop.

Ownership of properties is determined by whether the property belongs to the object directly and not to its prototype chain.

Properties of an object can also be retrieved in total.

There are a number of built-in means of detecting, iterating/enumerating, and retrieving object properties, with the chart showing which are available. Some sample code follows which demonstrates how to obtain the missing categories.

 

Property enumerability and ownership - built-in methods of detection, retrieval, and iteration
Functionality Own object Own object and its prototype chain Prototype chain only
Detection
Enumerable Nonenumerable Enumerable and Nonenumerable
propertyIsEnumerable hasOwnProperty and not propertyIsEnumerable hasOwnProperty
Not available without extra code Not available without extra code
Retrieval
Enumerable Nonenumerable Enumerable and Nonenumerable
Object.keys getOwnPropertyNames filtered to include properties when not passing propertyIsEnumerable getOwnPropertyNames
Not available without extra code Not available without extra code
Iteration
Enumerable Nonenumerable Enumerable and Nonenumerable
Iterate over Object.keys Iterate over getOwnPropertyNames filtered to include properties when not passing propertyIsEnumerable Iterate over getOwnPropertyNames
Enumerable Nonenumerable Enumerable and Nonenumerable
for..in Not available without extra code Not available without extra code
Not available without extra code

   

Obtaining properties by enumerability/ownership

// Note that this is not the most efficient algorithm for all cases, but useful for a quick demonstration
// Detection can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) > -1
// Iteration can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {}); // Or use filter(), map(), etc.

var SimplePropertyRetriever = {
    getOwnEnumerables: function (obj) {
        return this._getPropertyNames(obj, true, false, this._enumerable); // Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
    },
    getOwnNonenumerables: function (obj) {
        return this._getPropertyNames(obj, true, false, this._notEnumerable);
    },
    getOwnEnumerablesAndNonenumerables: function (obj) {
        return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable); // Or just use: return Object.getOwnPropertyNames(obj);
    },
    getPrototypeEnumerables: function (obj) {
        return this._getPropertyNames(obj, false, true, this._enumerable);
    },
    getPrototypeNonenumerables: function (obj) {
        return this._getPropertyNames(obj, false, true, this._notEnumerable);
    },
    getPrototypeEnumerablesAndNonenumerables: function (obj) {
        return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
    },
    getOwnAndPrototypeEnumerables: function (obj) {
        return this._getPropertyNames(obj, true, true, this._enumerable); // Or could use unfiltered for..in
    },
    getOwnAndPrototypeNonenumerables: function (obj) {
        return this._getPropertyNames(obj, true, true, this._notEnumerable);
    },
    getOwnAndPrototypeEnumerablesAndNonenumerables: function (obj) {
        return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
    },
    // Private static property checker callbacks
    _enumerable : function (obj, prop) {
        return obj.propertyIsEnumerable(prop);
    },
    _notEnumerable : function (obj, prop) {
        return !obj.propertyIsEnumerable(prop);
    },
    _enumerableAndNotEnumerable : function (obj, prop) {
        return true;
    },
    // Inspired by http://stackoverflow.com/a/8024294/271577
    _getPropertyNames : function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
        var props = [];

        do {
            if (iterateSelfBool) {
                Object.getOwnPropertyNames(obj).forEach(function (prop) {
                    if (props.indexOf(prop) === -1 && includePropCb(obj, prop)) {
                        props.push(prop);
                    }
                });
            }
            if (!iteratePrototypeBool) {
                break;
            }
            iterateSelfBool = true;
        } while (obj = Object.getPrototypeOf(obj));

        return props;
    }
};

Detection Table

  in hasOwnProperty propertyIsEnumerable in Object.keys in Object.getOwnPropertyNames
Enumerable true true true true true
Nonenumerable true true false false true
Inherited Enumerable true false false false false
Inherited Nonenumerable true false false false false

See also

 

Revision Source

<p>Enumerable properties are those which can be iterated by a for..in loop.</p>
<p>Ownership of properties is determined by whether the property belongs to the object directly and not to its prototype chain.</p>
<p>Properties of an object can also be retrieved in total.</p>
<p>There are a number of built-in means of detecting, iterating/enumerating, and retrieving object properties, with the chart showing which are available. Some sample code follows which demonstrates how to obtain the missing categories.</p>
<p>&nbsp;</p>
<table height="138" summary="Property enumerability and ownership - detection, retrieval, and iteration" width="598">
  <caption>
    Property enumerability and ownership - built-in methods of detection, retrieval, and iteration</caption>
  <tbody>
    <tr>
      <th>Functionality</th>
      <th>Own object</th>
      <th>Own object and its prototype chain</th>
      <th>Prototype chain only</th>
    </tr>
    <tr>
      <td>Detection</td>
      <td>
        <table summary="Enumerability">
          <thead>
            <tr>
              <th scope="col">Enumerable</th>
              <th scope="col">Nonenumerable</th>
              <th scope="col">Enumerable and Nonenumerable</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></td>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a>&nbsp;and not&nbsp;<a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></td>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>Not available without extra code</td>
      <td>Not available without extra code</td>
    </tr>
    <tr>
      <td>Retrieval</td>
      <td>
        <table summary="Enumerability">
          <thead>
            <tr>
              <th scope="col">Enumerable</th>
              <th scope="col">Nonenumerable</th>
              <th scope="col">Enumerable and Nonenumerable</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></td>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a> filtered to include properties when not passing <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></td>
              <td><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>Not available without extra code</td>
      <td>Not available without extra code</td>
    </tr>
    <tr>
      <td>Iteration</td>
      <td>
        <table summary="Enumerability">
          <thead>
            <tr>
              <th scope="col">Enumerable</th>
              <th scope="col">Nonenumerable</th>
              <th scope="col">Enumerable and Nonenumerable</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>Iterate over&nbsp;<a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></td>
              <td>Iterate over <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a> filtered to include properties when not passing <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></td>
              <td>Iterate over <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>
        <table summary="Enumerability">
          <thead>
            <tr>
              <th scope="col">Enumerable</th>
              <th scope="col">Nonenumerable</th>
              <th scope="col">Enumerable and Nonenumerable</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td><a href="/en-US/docs/JavaScript/Reference/Statements/for...in" title="/en-US/docs/JavaScript/Reference/Statements/for...in">for..in</a></td>
              <td>Not available without extra code</td>
              <td>Not available without extra code</td>
            </tr>
          </tbody>
        </table>
      </td>
      <td>Not available without extra code</td>
    </tr>
  </tbody>
</table>
<p>&nbsp;&nbsp;&nbsp;</p>
<h2 id="Obtaining_properties_by_enumerability.2Fownership">Obtaining properties by enumerability/ownership</h2>
<pre class="brush: js">
// Note that this is not the most efficient algorithm for all cases, but useful for a quick demonstration
// Detection can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).indexOf(prop) &gt; -1
// Iteration can occur by SimplePropertyRetriever.theGetMethodYouWant(obj).forEach(function (value, prop) {}); // Or use filter(), map(), etc.

var SimplePropertyRetriever = {
&nbsp;&nbsp;&nbsp; getOwnEnumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, false, this._enumerable); // Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getOwnNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, false, this._notEnumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getOwnEnumerablesAndNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable); // Or just use: return Object.getOwnPropertyNames(obj);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getPrototypeEnumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, false, true, this._enumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getPrototypeNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, false, true, this._notEnumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getPrototypeEnumerablesAndNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getOwnAndPrototypeEnumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, true, this._enumerable); // Or could use unfiltered for..in
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getOwnAndPrototypeNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, true, this._notEnumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; getOwnAndPrototypeEnumerablesAndNonenumerables: function (obj) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; // Private static property checker callbacks
&nbsp;&nbsp;&nbsp; _enumerable : function (obj, prop) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return obj.propertyIsEnumerable(prop);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; _notEnumerable : function (obj, prop) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return !obj.propertyIsEnumerable(prop);
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; _enumerableAndNotEnumerable : function (obj, prop) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;
&nbsp;&nbsp;&nbsp; },
&nbsp;&nbsp;&nbsp; // Inspired by http://stackoverflow.com/a/8024294/271577
&nbsp;&nbsp;&nbsp; _getPropertyNames : function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var props = [];

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; do {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (iterateSelfBool) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Object.getOwnPropertyNames(obj).forEach(function (prop) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (props.indexOf(prop) === -1 &amp;&amp; includePropCb(obj, prop)) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; props.push(prop);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!iteratePrototypeBool) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iterateSelfBool = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } while (obj = Object.getPrototypeOf(obj));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return props;
&nbsp;&nbsp;&nbsp; }
};</pre>
<h2 id="Detection_Table">Detection Table</h2>
<table align="center">
  <thead>
    <tr>
      <th scope="row">&nbsp;</th>
      <th scope="col">in</th>
      <th scope="col">hasOwnProperty</th>
      <th scope="col">propertyIsEnumerable</th>
      <th scope="col">in Object.keys</th>
      <th scope="col">in Object.getOwnPropertyNames</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Enumerable</th>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">true</td>
    </tr>
    <tr>
      <th scope="row">Nonenumerable</th>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">true</td>
    </tr>
    <tr>
      <th scope="row">Inherited&nbsp;Enumerable</th>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
    </tr>
    <tr>
      <th scope="row">Inherited Nonenumerable</th>
      <td style="text-align: center;">true</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
      <td style="text-align: center;">false</td>
    </tr>
  </tbody>
</table>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Operators/in" title="/en-US/docs/JavaScript/Reference/Operators/in">in</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for...in" title="/en-US/docs/JavaScript/Reference/Statements/for...in">for..in</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/hasOwnProperty">hasOwnProperty</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable">propertyIsEnumerable</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames">getOwnPropertyNames</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys" title="/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys">Object.keys</a></li>
</ul>
<p>&nbsp;</p>
Revert to this revision