I have used Object.defineProperty
and enumerable: false
to define a few properties on a config object. There is however one place in my module where I would like to iterate over the non-enumerable properties as well as the enumerable ones. Is it possible to do this without keeping a list of property names elsewhere?
Asked
Active
Viewed 2,544 times
13

wheresrhys
- 22,558
- 19
- 94
- 162
1 Answers
18
I guess you could use getOwnPropertyNames
which returns properties, enumerable or not.
From the docs:
Returns an array of all properties (enumerable or not) found directly upon a given object.

plalx
- 42,889
- 6
- 74
- 90
-
An example where I needed this in my project: Looping through all the Math functions/constants. var mathProps = Object.getOwnPropertyNames(Math); mathProps is then ["E", "LN10", "LN2" .... ] – Daniel Howard Nov 20 '14 at 11:38
-
@DanielHoward Yes, but it also includes `toSource`; which you probably don't want. In general, you'd need to be sure to filter the results to exactly what you expect (either with a whitelist or a blacklist), so it's probably only useful in introspection or serialization code, and things like that, where you're applying a general operation which doesn't care which properties are returned (or which object is passed to it), it just needs all of them. – jpaugh Sep 20 '17 at 17:50