我会简短地说:
我想枚举对象内部的所有可用函数和对象,window
以创建某种对象反射代码。
它在每个浏览器中都运行得很好,但在 Firefox 中。这是我的伪循环代码:
var all_names=[];
for (var i in window)
{
//if it's NOT an object
all_name.push(i.toString());
//if it's an object
enum up to 3 more levels in child objects.
}
而且我不想在 Firefox 中使用可用的 API,例如getOwnPropertyNames
.
所以我该怎么做?Javascript中的枚举有没有更好的解决方案(当然是跨浏览器)
以下是更多技术信息:
确切的 Firefox 错误:
[20:48:04.539] uncaught exception: [Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: "http://localhost/test/common.js Line: 53"]
确切的枚举循环代码:
function reflectAsString() {
try{
var m1 = "";
var m2 = "";
var m3 = "";
for (var i in window) {
if(window[i] && window[i]!= null && window[i] != "globalStorage")
{
m1 += i;
first_instance = window[i];
if(typeof first_instance == "object")
{
for(var j in first_instance)
{
if(first_instance[j] && first_instance[j]!= null)
{
m2 += j;
second_instance = first_instance[j];
if(typeof second_instance == "object")
{
for (var k in second_instance)
{
if(second_instance[k] && second_instance[k]!= null)
{
m3 += k;
}
}
}
}
}
}
}
}
return hex_md5(hex_md5(m1)+hex_md5(m2)+hex_md5(m3));
} catch(e) {
try {
var strToHash = Object.getOwnPropertyNames(window).filter(function(property) {
return typeof window[property] == 'function';
});
return hex_md5(strToHash.toString());
} catch(e2) {
return "undef";
}
}
}