MDN may have intermittent access issues April 18 13:00 - April 19 01:00 UTC. See whistlepig.mozilla.org for all notifications.

mozilla

Revision 71772 of Object.prototype.propertyIsEnumerable()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
  • Revision title: propertyIsEnumerable
  • Revision id: 71772
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment syntax coloring; no wording changes

Revision Content

 

Summary

Returns a boolean indicating whether the specified property is enumerable.

Syntax

obj.propertyIsEnumerable(prop);

Parameters

prop 
The name of the property to test.

Description

Every object has a propertyIsEnumerable method. This method can determine whether the specified property in an object can be enumerated by a for...in loop, with the exception of properties inherited through the prototype chain. If the object does not have the specified property, this method returns false.

Note: Starting in JavaScript 1.8.1 (in Firefox 3.6), propertyIsEnumerable("prototype") returns false instead of true; this makes the result compliant with ECMAScript 5.

Examples

Example: A basic use of propertyIsEnumerable

The following example shows the use of propertyIsEnumerable on objects and arrays:

var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';

o.propertyIsEnumerable('prop');   // returns true
a.propertyIsEnumerable(0);        // returns true

Example: User-defined versus built-in objects

The following example demonstrates the enumerability of user-defined versus built-in properties:

var a = ['is enumerable'];

a.propertyIsEnumerable(0);          // returns true
a.propertyIsEnumerable('length');   // returns false

Math.propertyIsEnumerable('random');   // returns false
this.propertyIsEnumerable('Math');     // returns false

Example: Direct versus inherited properties

var a = [];
a.propertyIsEnumerable('constructor');         // returns false

function firstConstructor()
{
  this.property = 'is not enumerable';
}

function secondConstructor()
{
  this.method = function method() { return 'is enumerable'; };
}

secondConstructor.prototype = new firstConstructor;

var o = new secondConstructor();
o.arbitraryProperty = 'is enumerable';

o.propertyIsEnumerable('arbitraryProperty');   // returns true
o.propertyIsEnumerable('method');              // returns true
o.propertyIsEnumerable('property');            // returns false

o.property = 'is enumerable';

o.propertyIsEnumerable('property');            // returns true

See also

for...in

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

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Returns a boolean indicating whether the specified property is enumerable.</p>
<h3 name="Syntax">Syntax</h3>
<p><code><em>obj</em>.propertyIsEnumerable(<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 has a <code>propertyIsEnumerable</code> method. This method can determine whether the specified property in an object can be enumerated by a <a href="/en/Core_JavaScript_1.5_Reference/Statements/for...in" title="en/Core_JavaScript_1.5_Reference/Statements/for...in"><code>for...in</code></a> loop, with the exception of properties inherited through the prototype chain. If the object does not have the specified property, this method returns false.</p>
<div class="note"><strong>Note:</strong> Starting in JavaScript 1.8.1 (in Firefox 3.6), <code>propertyIsEnumerable("prototype")</code> returns false instead of true; this makes the result compliant with ECMAScript 5.</div>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_A_basic_use_of_propertyIsEnumerable">Example: A basic use of <code>propertyIsEnumerable</code></h4>
<p>The following example shows the use of <code>propertyIsEnumerable</code> on objects and arrays:</p>
<pre class="brush: js">var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';

o.propertyIsEnumerable('prop');   // returns true
a.propertyIsEnumerable(0);        // returns true
</pre>
<h4 name="Example:_User-defined_versus_built-in_objects">Example: User-defined versus built-in objects</h4>
<p>The following example demonstrates the enumerability of user-defined versus built-in properties:</p>
<pre class="brush: js">var a = ['is enumerable'];

a.propertyIsEnumerable(0);          // returns true
a.propertyIsEnumerable('length');   // returns false

Math.propertyIsEnumerable('random');   // returns false
this.propertyIsEnumerable('Math');     // returns false
</pre>
<h4 name="Example:_Direct_versus_inherited_properties">Example: Direct versus inherited properties</h4>
<pre class="brush: js">var a = [];
a.propertyIsEnumerable('constructor');         // returns false

function firstConstructor()
{
  this.property = 'is not enumerable';
}

function secondConstructor()
{
  this.method = function method() { return 'is enumerable'; };
}

secondConstructor.prototype = new firstConstructor;

var o = new secondConstructor();
o.arbitraryProperty = 'is enumerable';

o.propertyIsEnumerable('arbitraryProperty');   // returns true
o.propertyIsEnumerable('method');              // returns true
o.propertyIsEnumerable('property');            // returns false

o.property = 'is enumerable';

o.propertyIsEnumerable('property');            // returns true
</pre>
<h3 name="See_also">See also</h3>
<p><a href="/en/Core_JavaScript_1.5_Reference/Statements/for...in" title="en/Core_JavaScript_1.5_Reference/Statements/for...in">for...in</a></p>

<p>{{ languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/propertyIsEnumerable", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable" } ) }}</p>
Revert to this revision