I have a keen interest in both enumerable and non-enumerable properties.
Enumerating things that are not enumerable is a challenge. However, it is possible to define non-enumerable properties to prevent them from showing up in iterators like for...in
or Object.keys
. To find out more about this workaround, check out this Q&A: Is it possible to get the non-enumerable inherited property names of an object?
Currently, the simplest way to iterate through all properties, including both own properties and prototype properties, is by using a for...in
loop:
for(var propertyName in obj) {
}
You can also utilize Object.hasOwnProperty
to verify if a property does not belong to the object's prototype:
for(var propertyName in obj) {
if(obj.hasOwnProperty(propertyName)) {
// This property is not part of the object's prototype...
}
}