Object.prototype.propertyIsEnumerable()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
  • Revision title: propertyIsEnumerable
  • Revision id: 71765
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[ja:Core JavaScript 1.5 Reference:Global Objects:Object:propertyIsEnumerable]]

Revision Content

Summary

Returns a boolean indicating whether the specified property is enumerable.

Syntax

propertyIsEnumerable(prop);

Parameters

prop 
The name of the property to test.

Description

Every object descended from Object has the propertyIsEnumerable method. This method can determine whether an object's property can be enumerated by a for...in loop. If the object does not have the specified property, this method returns false. Although for...in does consider the object's prototype chain, this method cannot be used on an object's property that has been inherited through the prototype chain.

Examples

Example: A basic use of propertyIsEnumerable

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

o = new Object();
a = new Array();
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:

a = new Array('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

a = new Array();
a.propertyIsEnumerable('constructor');   // returns false

function myConstructor() {
  this.isnot = 'not enumerable';
}

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

secondConstructor.prototype = new myConstructor;

o = new secondConstructor();
o.prop = 'is';

o.propertyIsEnumerable('is');      // returns true
o.propertyIsEnumerable('prop');    // returns true
o.propertyIsEnumerable('isnot');   // returns false

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>
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 descended from <code>Object</code> has the <code>propertyIsEnumerable</code> method. This method can determine whether an object's property can be enumerated by a <a href="en/Core_JavaScript_1.5_Reference/Statements/for...in"><code>for...in</code></a> loop. If the object does not have the specified property, this method returns false. Although <code>for...in</code> does consider the object's prototype chain, this method cannot be used on an object's property that has been inherited through the prototype chain.
</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>o = new Object();
a = new Array();
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>a = new Array('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>a = new Array();
a.propertyIsEnumerable('constructor');   // returns false

function myConstructor() {
  this.isnot = 'not enumerable';
}

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

secondConstructor.prototype = new myConstructor;

o = new secondConstructor();
o.prop = 'is';

o.propertyIsEnumerable('is');      // returns true
o.propertyIsEnumerable('prop');    // returns true
o.propertyIsEnumerable('isnot');   // returns false
</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