列出窗口对象的所有属性? [英] List all properties of window object?

查看:97
本文介绍了列出窗口对象的所有属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望(动态)获取浏览器当前知道的HTML元素列表,例如 HTMLPreElement HTMLSpanElement 等。这些对象是全局的,即

I'm looking to (dynamically) obtain a list of HTML elements the browser is currently aware of, such as HTMLPreElement, HTMLSpanElement etc. These objects are global, i.e.

console.log('HTMLPreElement' in window);  //=> true

所以我认为我可以使用 getOwnPropertyNames 像这样:

So I thought I'd be able to use getOwnPropertyNames like this:

console.log(Object.getOwnPropertyNames(window));

获取全局属性的完整列表(MDN声明它返回可枚举和不可枚举的属性)。

to obtain the full list of global properties (MDN states that this returns both enumerable and non-enumerable properties).

使用上面的内容,我得到一个包含大约70个属性区域的数组。但是,它不包括像 HTMLPreElement 这样的对象 - 只有 HTMLElement 。我也尝试过:

Using the above, I get an array with around 70 property nanes. But, it doesn't include objects like HTMLPreElement - only HTMLElement. I also tried:

console.log(Object.getOwnPropertyNames(window.Window.prototype));

带回更大的清单(包括 addEventListener 等)但是再一次,没有 HTMLPreElement

which brings back a bigger list (including addEventListener etc) but again, no HTMLPreElement.

那么,这些什么地方 HTML {Tag}元素对象驻留?

So, where the heck do these HTML{Tag}Element objects reside?

推荐答案

在Firefox中,除非明确要求作为全局变量,否则不会添加其全局对象的元素行为或财产。也许Firefox懒惰地将它们加载到环境中,这样它们就不会消耗内存,除非实际需要它们。

In Firefox, it seems to be the behavior of elements that their global object is not added unless explicitly requested as a global variable or property. Perhaps Firefox lazy loads them into the environment so that they don't consume memory unless they're actually needed.

当通过 Object.getOwnPropertyNames <只是简单地请求全局对象的键时,它们似乎不显示em>除非它们首先被如上所述明确引用。

It seems that they do not show up when simply requesting the keys of the global object via Object.getOwnPropertyNames unless they've first been explicitly referenced as described above.

http://jsfiddle.net/mBAHm/

这篇关于列出窗口对象的所有属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆