Core JavaScript 1.5 Reference:Global Objects:Object:propertyIsEnumerable
From MDC
Contents |
[edit] Summary
Returns a boolean indicating whether the specified property is enumerable.
[edit] Syntax
obj.propertyIsEnumerable(prop);
[edit] Parameters
-
prop - The name of the property to test.
[edit] 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.
[edit] Examples
[edit] 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
[edit] 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
[edit] 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