Equivalent of Python's dir in Javascript Equivalent of Python's dir in Javascript python python

Equivalent of Python's dir in Javascript


There is keys method in Object, for example:

Object.keys(object)

But this return object's own properties and methods only.
To list all properties and methods of an object I know 2 possibilities:

  1. console.dir(object) method in firebug console for Firefox and
  2. dir(object) method in Google Chrome development tools.


This may work for you, if you need a simple solution:

function dir(object) {    stuff = [];    for (s in object) {        stuff.push(s);    }    stuff.sort();    return stuff;}


There are a couple of functions which do just this in the code for ChatZilla, you'll have to check the licence properly to see if you can just rip them out and use them wherever.

The relevant functions can be found athttp://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136dumpObject and dumpObjectTree