1

when working with frameworks(in this case Phaser.js) I like to use the console and look for possible properties of Objects with

Object.getOwnPropertyNames(myObject)

However, some properties wont show up in the console and I assume is due to not being "OwnPropertyNames" of the object. Is there a way to still get them to show in the console?

I´m basically looking for something like

Object.getAllPropertyNames(myObject)

Could someone help me out? I´m new to JS :)

Faizy
  • 299
  • 2
  • 12
  • 1
    Look at `Object.keys`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys – Palpatim Apr 16 '17 at 18:38

2 Answers2

2

If you are using Google Chrome's DevTools, you can view all properties of an object by calling dir(myObject):

enter image description here

user7401478
  • 1,372
  • 1
  • 8
  • 25
  • This is cool - thanks a lot! Could you explain me why the properties(on the bottom) are in a lighter blue? What does this mean? Are they inheritated? These properties wont show up when using "Object.getOwnProperties(game.stage);" https://i.gyazo.com/18dd2063923cc159de46abb9f386832b.png – Faizy Apr 16 '17 at 18:42
  • @Faizy What do you mean "in blue"? In the picture property keys are purple. – user7401478 Apr 16 '17 at 18:44
  • @Faizy Light-purple properties are [non-enumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable) – user7401478 Apr 16 '17 at 18:49
  • @Faizy **Non-enumerable properties are not visible in for-loops:** http://stackoverflow.com/questions/17893718/what-does-enumerable-mean – user7401478 Apr 16 '17 at 18:51
  • 1
    Thanks. But shouldn´t they still pop up using `Object.getOwnPropertyNames()`? It says "The Object.getOwnPropertyNames() method returns an array of all properties (enumerable or not) found directly upon a given object." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames – Faizy Apr 16 '17 at 18:57
1

Just use a for ... in loop to loop over all the properties. This will also loops over the "parent" properties.

// example data
var o = { 
  a: "a1",
  b: "b1"
}

var parent = { 
  c: "c1",
  d: "d1"
}
// set parent, just for the example
Object.setPrototypeOf(o, parent);
  
// loop over all
for (key in o)
{
  console.log('prop "' + key + '" with value "' + o[key] + '"');
}

Nested example:

var game =  {
  stage :{
    test1 : 'v1',
    test2 : 'v2'
    
  }
   
}

for (key in game.stage) 
 { console.log(key + '" with value "' + game.stage[key] + '"'); }
Julian
  • 33,915
  • 22
  • 119
  • 174
  • Thanks Julian. If I have the object "game" and it has the property "stage", how can I use the for ... in loop to get every property in/behind "game.stage"? – Faizy Apr 16 '17 at 18:48
  • `for (key in game.stage) ...` – Julian Apr 16 '17 at 18:49
  • If I try it like this `for (key in game.stage) { console.log(key + '" with value "' + game.stage[key] + '"'); }` the console returns "nothing"`... – Faizy Apr 16 '17 at 18:53
  • added the `game.stage` example. Maybe the script is running to soon? (should run after DOM loaded) – Julian Apr 16 '17 at 19:08