2

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?

Asciiom
  • 9,867
  • 7
  • 38
  • 57
Ido Ofir
  • 207
  • 2
  • 8
  • http://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object – Brad Sep 22 '12 at 14:22

1 Answers1

1

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)

Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • Note that this is only available in Javascript 1.8.5 or later. (ES5) – jonvuri Sep 22 '12 at 14:24
  • It is, thanks, I'll add it to my answer for completeness sake – Asciiom Sep 22 '12 at 14:29
  • thats pretty good, thanks! so this returns properties that are not inherited.. it does not count for functions though, like for-in does.. but it's interesting to see that event handlers (like onclick) are not considered functions unless they actually have a function assigned to them.(if you check their type they claim to be an object..) – Ido Ofir Sep 23 '12 at 06:38