Equivalent of Python's dir in Javascript

让人想犯罪 __ 提交于 2019-11-26 22:55:40

问题


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?


回答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




回答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.




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



回答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




回答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.




回答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



来源:https://stackoverflow.com/questions/5523747/equivalent-of-pythons-dir-in-javascript

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