在Javascript中相当于Python的目录

时间:2022-04-26 12:56:44

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 console等交互式控制台从浏览器开发Javascript代码时,如何以编程方式获得相同的信息?

6 个解决方案

#1


7  

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#l136 dumpObject and dumpObjectTree

相关的函数可以在http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136上找到dumpObject和dumpObjectTree

#2


21  

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.

但这只返回对象自己的属性和方法。要列出对象的所有属性和方法,我知道两种可能性:1。Firefox中的firebug控制台中的console.dir(对象)方法和Google Chrome开发工具中的2.dir(对象)方法。

#3


12  

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


3  

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

#5


2  

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)

全局变量保存在易于访问的对象(窗口)中,因此您可以轻松地检查/迭代它们。 (使用类似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


1  

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

好吧,你可以看到对象包含只有它自己的属性:通过它可以在任何控制台工作不仅谷歌chrome web浏览器寻找img输入图像描述这里console.dir(obj);链接:https://developers.google.com/web/tools/chrome-devtools/console/console-reference

#1


7  

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#l136 dumpObject and dumpObjectTree

相关的函数可以在http://hg.mozilla.org/chatzilla/file/59b46c0bf716/js/lib/utils.js#l136上找到dumpObject和dumpObjectTree

#2


21  

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.

但这只返回对象自己的属性和方法。要列出对象的所有属性和方法,我知道两种可能性:1。Firefox中的firebug控制台中的console.dir(对象)方法和Google Chrome开发工具中的2.dir(对象)方法。

#3


12  

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


3  

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

#5


2  

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)

全局变量保存在易于访问的对象(窗口)中,因此您可以轻松地检查/迭代它们。 (使用类似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


1  

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

好吧,你可以看到对象包含只有它自己的属性:通过它可以在任何控制台工作不仅谷歌chrome web浏览器寻找img输入图像描述这里console.dir(obj);链接:https://developers.google.com/web/tools/chrome-devtools/console/console-reference