Object.prototype.propertyIsEnumerable()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
  • Revision title: propertyIsEnumerable
  • Revision id: 71769
  • Created:
  • Creator: Sevenspade
  • Is current revision? No
  • Comment /* Syntax */ formatting

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.

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

{{ wiki.languages( { "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><i>obj</i>.propertyIsEnumerable(<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 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"><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>
<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="eval">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="eval">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="eval">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">for...in</a>
</p>{{ wiki.languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/propertyIsEnumerable" } ) }}
Revert to this revision