Equivalent of Python’s dir in Javascript

Python Programming

Question or problem about Python programming:

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?

How to solve the problem:

Solution 1:

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

Solution 2:

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.

Solution 3:

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 4:

The Google Chrome developer tools console has a predefined dir: https://developers.google.com/chrome-developer-tools/docs/console

Firebug has console.dir: http://getfirebug.com/logging

Solution 5:

The global variables are kept in an easily accessible object (window) and so you can inspect/iterate over them easily. (Using something like the functions suggested by Glenjamin)

On the other hand, I don’t know of any way to inspect local variables defined in functions or closures – if this is possible I’d at least guess it would be highly browser/console specific.

Solution 6:

well you can see object contains like its own properties only : By
it can work in any console not only google chrome web browser look for the img enter image description here
console.dir(obj);
here link: https://developers.google.com/web/tools/chrome-devtools/console/console-reference

Hope this helps!