Ido Ofir
Ido Ofir

Reputation: 227

can members of javascript objects be iterated in full?

I would like to be able to iterate through all the members of an object. something like this:

 function reflect(obj) {
 var str = "";
 for (member in obj) { str += (member + "\n"); }
 return str;
 }

but the Enumerable flag prevents many of the members to apear in the for in loop. my question is:

  1. is there another way to iterate through an object's members that exposes all of them?

  2. if not, is there some access to these flags? (can I set Enumerable to true?)

  3. is there a way to expose the prototype chain and determine which member belongs to which ancestor?

Upvotes: 2

Views: 54

Answers (1)

Asciiom
Asciiom

Reputation: 9975

You can use getOwnPropertyNamesfor that. It returns all properties regardless of the enumerable option.

var objectProperties = Object.getOwnPropertyNames(obj);

Update This is only available for Javascript 1.8.5 and newer! (thanks @Kiyura)

Upvotes: 1

Related Questions