Equivalent of Python's dir in Javascript

北战南征 提交于 2019-11-27 21:00:58

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

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.

dmitry_romanov

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;
}

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!