【发布时间】:2014-06-24 12:35:58
【问题描述】:
我想枚举所有全局对象。这包括 HTMLElement、SVGAnimatedPreserveAspectRatio 和 CameraControl。我为这个问题看到的所有答案都让您遍历window 或global 对象(通过命名空间外的this 等技巧获得)。然而,这些技巧并没有列出元素,即使它包含在 window!
这是一些控制台日志,但如果你把它放在一个测试 .html 页面中,你会得到相同的结果。
> HTMLElement
[object Function]
> window['HTMLElement']
[object Function]
> 'HTMLElement' in window
true
> for(var name in window){if(name == "HTMLElement")console.log('Found it!');}
undefined
> for(var name in window){if(name == "sessionStorage")console.log('Found it!');}
"Found it!"
如何枚举所有全局对象?
【问题讨论】:
-
var a = Object.getOwnPropertyNames(window);->a.indexOf("HTMLElement")-> 475(对我来说)
标签: javascript dom