1

if I iterate over an object, I need to check whether it is not a prototype object I loop over. (with hasOwnProperty)

If I collect the keys with Object.keys, I always just get the "real keys" back. Is this correct?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Christian
  • 6,961
  • 10
  • 54
  • 82

1 Answers1

1

Yes, Object.keys() gives you the keys of the properties directly on the object. So you could use that.

Keep in mind that Object.keys() may not be supported in every one of your target browsers if you're doing front-end development.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks you! Tha means it's always better to do this: for( var i in Object.keys(obj)) than doing this: for(var i in obj) because the prototype attributes are filtered out? – Christian Aug 14 '14 at 07:12
  • @Christian I'd use a normal `for` loop, as the result is an Array. You could still be bitten by enumerable properties from extending the Object or Array prototype otherwise. – alex Aug 14 '14 at 07:28