Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over non-enumerable properties

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?

like image 661
wheresrhys Avatar asked Aug 30 '13 17:08

wheresrhys


People also ask

What are the properties of enumerable set?

Enumerable properties are those properties whose internal enumerable flag is set to true, which is the default for properties created via simple assignment or via a property initializer. Properties defined via Object. defineProperty and such are not enumerable by default.

What line of code is used to iterate through all the properties?

The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

What are non-enumerable properties?

Objects can have properties that don't show up when iterated through the particular object using Object. keys() or for...in loop. Those type of properties are called as non-enumerable properties.


1 Answers

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.

like image 84
plalx Avatar answered Oct 19 '22 13:10

plalx