相当于 Javascript 中 Python 的目录
当我从解释器编写 Python 代码时,我可以输入 dir() 来获得当前作用域中定义的名称列表。当我使用 Firebug、Chrome 控制台等交互式控制台从浏览器开发 Javascript 代码时,如何以编程方式获得相同的信息?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Object
中有keys
方法,例如:但是这个只返回对象自己的属性和方法。
要列出对象的所有属性和方法,我知道有两种可能性:
console.dir(object)
方法和dir(object)
方法工具。There is
keys
method inObject
, for example:But this return object's own properties and methods only.
To list all properties and methods of an object I know 2 possibilities:
console.dir(object)
method in firebug console for Firefox anddir(object)
method in Google Chrome development tools.如果您需要一个简单的解决方案,这可能对您有用:
This may work for you, if you need a simple solution:
在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
anddumpObjectTree
您可以使用几个功能来获取所需的数据。
object.keys()
此功能将返回所有 em> 符号。
Object.getOwnPropertyNames()
此函数将返回所有可枚举和不可枚举的属性,其中 >不是符号。
当我们有
Object.keys()
时,为什么这个函数很有用?顺便说一句,为什么
Object.getOwnPropertyNames(Set)
不为您提供Set
上的方法,例如add
、有
等,?因为它们位于Set.prototype
上。Object.getOwnPropertyNames(Set.prototype)
将产生更好的结果。Object.getOwnPropertySymbols()
这将返回您传递给它的对象中
Symbol
的所有拥有属性到。Reflect.ownKeys()
这将返回所有拥有的属性,即字符串/符号 在您传递给的对象中。
奖励:
Object.getPrototypeOf()
这将返回传递给它的对象的
Prototype
。使用它,我们可以递归地枚举对象及其原型链的所有属性。
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.
Object.getOwnPropertyNames()
This function will return all properties that both enumerable and non-enumerable which are not Symbols.
Why is this function useful when we have
Object.keys()
?As an aside, why doesn't
Object.getOwnPropertyNames(Set)
doesn't give you the methods onSet
likeadd
,has
, etc., ? Because they are onSet.prototype
.Object.getOwnPropertyNames(Set.prototype)
will yield a much better result.Object.getOwnPropertySymbols()
This will return all the owned properties that are
Symbol
s in the Object you pass it to.Reflect.ownKeys()
This will return all the owned properties that are strings/Symbols in the object you pass it to.
Bonus:
Object.getPrototypeOf()
This will return the
Prototype
of the Object that is passed to it.Using this we can enumerate all the properties of an object and its prototype chain recursively.
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
全局变量保存在易于访问的对象(
窗口
)中,因此您可以轻松检查/迭代它们。 (使用类似 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.
好吧,你可以看到对象只包含它自己的属性:
它可以在任何控制台中工作,而不仅仅是 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
(只是为了查看该列表)
您可以使用运算符“.”,例如:
(just to see that list)
you can use the operator ".", for example:
真正的解决方案
首先,创建一个列出对象的所有属性的函数:
然后,就这么简单,调用该函数就像console.log(dir(console))
The Real Solution
First, create a function that lists out all the properties of an object:
Then, as easy as it is, call the function like
console.log(dir(console))