Equivalent of Python's dir in Javascript
when I write Python code from the interpreter I can type dir()
to have a list of names defined in the current scope. How can achieve to have the same information, programmatically, when I develop Javascript code from a browser using an interactive console like firebug, chrome console, etc?
Solution 1:
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:
-
console.dir(object)
method in firebug console for Firefox and -
dir(object)
method in Google Chrome development tools.
Solution 2:
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;
}
Solution 3:
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 at
http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136
dumpObject
and dumpObjectTree