相当于 Javascript 中 Python 的目录

发布于 2024-10-29 06:28:54 字数 123 浏览 2 评论 0 原文

当我从解释器编写 Python 代码时,我可以输入 dir() 来获得当前作用域中定义的名称列表。当我使用 Firebug、Chrome 控制台等交互式控制台从浏览器开发 Javascript 代码时,如何以编程方式获得相同的信息?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

很酷又爱笑 2024-11-05 06:28:54

Object中有keys方法,例如:

Object.keys(object)

但是这个只返回对象自己的属性和方法。
要列出对象的所有属性和方法,我知道有两种可能性:

  1. Firefox 的 firebug 控制台中的 console.dir(object) 方法和
  2. Google Chrome 开发中的 dir(object) 方法工具。

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.
痴情换悲伤 2024-11-05 06:28:54

如果您需要一个简单的解决方案,这可能对您有用:

function dir(object) {
    stuff = [];
    for (s in object) {
        stuff.push(s);
    }
    stuff.sort();
    return stuff;
}

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;
}
生来就爱笑 2024-11-05 06:28:54

在Chatzilla的代码中,有几个功能仅此功能,您必须正确检查许可证,以查看是否可以将其撕开并在任何地方使用它们。

相关功能可以在

dumpObject dumpObjectTree

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

痴者 2024-11-05 06:28:54

您可以使用几个功能来获取所需的数据。

object.keys()

此功能将返回所有 em> 符号。

> let person = {name: 'John Doe', age: 25, [Symbol('Test')] : 'value'}
> Object.keys(person);
['name'] // Note that the Symbol('Test') is not in the returned array!

Object.getOwnPropertyNames()

此函数将返回所有可枚举不可枚举的属性,其中 >不是符号。

> Object.getOwnPropertyNames(Set)
[ 'length', 'name', 'prototype' ]

当我们有 Object.keys() 时,为什么这个函数很有用?

> Object.keys(Set)
[] // Because keys doesn't give you non-enumerable properies

顺便说一句,为什么 Object.getOwnPropertyNames(Set) 不为您提供 Set 上的方法,例如 add等,?因为它们位于 Set.prototype 上。 Object.getOwnPropertyNames(Set.prototype) 将产生更好的结果。

Object.getOwnPropertySymbols()

这将返回您传递给它的对象中 Symbol 的所有拥有属性到。

> let person = {x: 10, Symbol('Test'): 'Test-value' };
> Object.getOwnPropertySymbols(person);
[Symbol(Test)]

Reflect.ownKeys()

这将返回所有拥有的属性,即字符串/符号 在您传递给的对象中。

> let person = {x: 1, [Symbol('Test')]: 'Test-value'};
> Reflect.ownKeys(person);
[ 'x', Symbol(Test) ]

奖励:

Object.getPrototypeOf()

这将返回传递给它的对象的Prototype

> let nameable = { name: 'name' };
> let ageable = Object.create(nameable);
> ageable.age = 0;
> let person = Object.create(ageable);
> let proto_of_person = Object.getPrototypeOf(person);
> proto_of_person === ageable;
true
> let proto_of_ageable = Object.getPrototypeOf(proto_of_person);
> proto_of_ageable === nameable
true

使用它,我们可以递归地枚举对象及其原型链的所有属性。

There are a couple of functions that you you can use to get the data that you need.

Object.keys()

This function will return all enumerable, owned properties that are not Symbols.

> let person = {name: 'John Doe', age: 25, [Symbol('Test')] : 'value'}
> Object.keys(person);
['name'] // Note that the Symbol('Test') is not in the returned array!

Object.getOwnPropertyNames()

This function will return all properties that both enumerable and non-enumerable which are not Symbols.

> Object.getOwnPropertyNames(Set)
[ 'length', 'name', 'prototype' ]

Why is this function useful when we have Object.keys()?

> Object.keys(Set)
[] // Because keys doesn't give you non-enumerable properies

As an aside, why doesn't Object.getOwnPropertyNames(Set) doesn't give you the methods on Set like add, has, etc., ? Because they are on Set.prototype. Object.getOwnPropertyNames(Set.prototype) will yield a much better result.

Object.getOwnPropertySymbols()

This will return all the owned properties that are Symbols in the Object you pass it to.

> let person = {x: 10, Symbol('Test'): 'Test-value' };
> Object.getOwnPropertySymbols(person);
[Symbol(Test)]

Reflect.ownKeys()

This will return all the owned properties that are strings/Symbols in the object you pass it to.

> let person = {x: 1, [Symbol('Test')]: 'Test-value'};
> Reflect.ownKeys(person);
[ 'x', Symbol(Test) ]

Bonus:

Object.getPrototypeOf()

This will return the Prototype of the Object that is passed to it.

> let nameable = { name: 'name' };
> let ageable = Object.create(nameable);
> ageable.age = 0;
> let person = Object.create(ageable);
> let proto_of_person = Object.getPrototypeOf(person);
> proto_of_person === ageable;
true
> let proto_of_ageable = Object.getPrototypeOf(proto_of_person);
> proto_of_ageable === nameable
true

Using this we can enumerate all the properties of an object and its prototype chain recursively.

佼人 2024-11-05 06:28:54

Google Chrome 开发者工具控制台有一个预定义目录:https://developers.google。 com/chrome-developer-tools/docs/console

Firebug 有 console.dir: http://getfirebug.com/日志记录

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

木落 2024-11-05 06:28:54

全局变量保存在易于访问的对象(窗口)中,因此您可以轻松检查/迭代它们。 (使用类似 Glenjamin 建议的函数)

另一方面,我不知道有什么方法可以检查函数或闭包中定义的局部变量 - 如果这是可能的,我至少猜测它会是高度浏览器/控制台具体的。

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.

傾旎 2024-11-05 06:28:54

好吧,你可以看到对象只包含它自己的属性:
它可以在任何控制台中工作,而不仅仅是 google chrome 网络浏览器查找 img 在此处输入图像描述
控制台.dir(obj);
此处链接:https://developers.google.com/web /tools/chrome-devtools/console/console-reference

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

夜唯美灬不弃 2024-11-05 06:28:54

(只是为了查看该列表)

您可以使用运算符“.”,例如:

> var a = "asdfg";
> a. // -> show the list
  • 在 chrome 控制台中,它将向您显示在 node.js 控制台中自动完成的选项列表,
  • 您可以执行相同的操作然后按两次 Tab 键即可查看列表

(just to see that list)

you can use the operator ".", for example:

> var a = "asdfg";
> a. // -> show the list
  • in the chrome console it will show you the list of options for autocompletion
  • in node.js console you can do the same and press tab twice to see the list
紙鸢 2024-11-05 06:28:54

真正的解决方案

首先,创建一个列出对象的所有属性的函数:

function dir(object) {
    props = [];
    for (prop in object) {
        props.push(prop);
    }
    props.sort();
    return props;
}

然后,就这么简单,调用该函数就像console.log(dir(console))

The Real Solution

First, create a function that lists out all the properties of an object:

function dir(object) {
    props = [];
    for (prop in object) {
        props.push(prop);
    }
    props.sort();
    return props;
}

Then, as easy as it is, call the function like console.log(dir(console))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文