相当于 Javascript 中 Python 的目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5523747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of Python's dir in Javascript
提问by Paolo
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?
当我从解释器编写 Python 代码时,我可以键入dir()
在当前范围内定义的名称列表。当我使用交互式控制台(如 firebug、chrome 控制台等)从浏览器开发 Javascript 代码时,如何以编程方式获得相同的信息?
采纳答案by Glenjamin
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.
ChatZilla 的代码中有几个功能可以做到这一点,您必须正确检查许可证以查看是否可以将它们撕掉并在任何地方使用它们。
The relevant functions can be found at
http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136dumpObject
and dumpObjectTree
相关函数可以在
http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136dumpObject
和dumpObjectTree
回答by Andrey
There is "keys" method in Object, for example:
Object中有“keys”方法,例如:
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.
但是这个只返回对象自己的属性和方法。
要列出对象的所有属性和方法,我知道 2 种可能性:
1. Firefox 的 firebug 控制台中的 console.dir(object) 方法和
Google Chrome 开发工具中的 dir(object) 方法。
回答by 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;
}
回答by Protector one
The Google Chrome developer tools console has a predefined dir: https://developers.google.com/chrome-developer-tools/docs/console
Google Chrome 开发者工具控制台有一个预定义的目录:https: //developers.google.com/chrome-developer-tools/docs/console
Firebug has console.dir: http://getfirebug.com/logging
Firebug 有 console.dir:http: //getfirebug.com/logging
回答by hugomg
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)
全局变量保存在一个易于访问的对象 ( window
) 中,因此您可以轻松地检查/迭代它们。(使用类似 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.
另一方面,我不知道有什么方法可以检查函数或闭包中定义的局部变量——如果可能的话,我至少猜测它是高度特定于浏览器/控制台的。
回答by AmitNayek
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 hereconsole.dir(obj); here link: https://developers.google.com/web/tools/chrome-devtools/console/console-reference
那么你可以看到对象只包含它自己的属性:通过它可以在任何控制台中工作,不仅谷歌浏览器网络浏览器查找 img在此处输入图像描述console.dir(obj); 这里链接:https: //developers.google.com/web/tools/chrome-devtools/console/console-reference