简体   繁体   English

所有内置的javascript对象

[英]All built-in javascript Objects

I need to get list of all javascript objects. 我需要获取所有javascript对象的列表。 In other words i need all NameOfXObject, for which such x exists, that: 换句话说,我需要所有这样的x存在的NameOfXObject:

x+""
will return "[object NameOfXObject]"

for example: 例如:

document+""         --> "[object HTMLDocument]"
window+""           --> "[object Window]"
locationStorage+""  --> "[object Storage]"

To sumarize: I need list containing: "HTMLDocument", "Window", "Storage"... 要sumarize:我需要包含以下内容的列表:“HTMLDocument”,“Window”,“Storage”......

I've tried 我试过了

Object.keys(window)

But it not returns all objects 但它不会返回所有对象

Thanks in advance, sorry for my english 提前谢谢,抱歉我的英语

You need to traverse the prototype chain of window with Object.getOwnPropertyNames to get all the properties attached to it. 您需要使用Object.getOwnPropertyNames遍历window的原型链,以获取附加到它的所有属性。

var target = window,
    result = [];

do {
    result = result.concat(Object.getOwnPropertyNames(target));
} while(target = Object.getPrototypeOf(target))

result = result
    //Filter out properties such as "onclick" which are null by default
    .filter(function(r){ return window[r] != null; }) 
    //Optional: filter out those whose type is not "object"
    .filter(function(r){ return typeof window[r] === 'object'; }); 

For me (Chrome v50 in this very page) filtering the results to only include property names whose length is 24 returned 对我来说(此页面中的Chrome v50)将结果过滤为仅包含返回长度为24的属性名称

["SpeechSynthesisUtterance", "PresentationAvailability", "BeforeInstallPromptEvent", "CanvasRenderingContext2D", "SVGRadialGradientElement", "SVGLinearGradientElement", "SVGFEGaussianBlurElement", "SVGFEDistantLightElement", "SVGAnimatedTransformList"]

but all of those are functions instead of objects and thus for those x+'' will give something more like "function SpeechSynthesisUtterance() { [native code] }" so I'm not sure if it contains what you were trying to find. 但所有这些都是函数而不是对象,因此对于那些x+''会给出更像"function SpeechSynthesisUtterance() { [native code] }"所以我不确定它是否包含你想要找到的东西。


To get a list containing the string representations like ['[object Math]', ...] , add 要获取包含['[object Math]', ...]等字符串表示的列表,请添加

    //Get the string representation of the global object
    .map(function(r){ return window[r] + ''; });

For me, one of those had the length of 24: '[object SpeechSynthesis]' 对我来说,其中一个长度为24: '[object SpeechSynthesis]'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM