出于测试目的,我想列出页面上设置的所有全局变量。最初我需要运行此代码的唯一浏览器是PhantomJS(基于webkit)。但是如果可能的话,我可能需要稍后在IE和Firefox上运行它。
到目前为止我所拥有的是:windowKnowAttributes = ['history','locationbar'....] // this will get huge.
for( v in window ){
if( window.hasOwnProperty(v) ) //AND
if( windowKnowAttributes.indexOf(v) === -1 )
console.log(v)
}
答案 0 :(得分:0)
您可以使用Array.filter()
将已知属性数组与窗口对象上的当前属性集进行比较...也许是这样:
var windowKnownProperties = ['history','locationbar'....]
var windowAllProperties = [];
for( v in window ){
windowAllProperties.push(v);
}
var unknownGlobals = windowAllProperties.filter(function(el, idx, arr) {
return (windowKnownProperties.indexOf(el) === -1);
});
您可能希望将函数包装在函数中,以便此处使用的变量本身不会在全局空间中结束。您还可以在运行任何其他脚本之前使用windowKnownProperties
循环生成for...in
,然后在页面加载后创建一个新的window
属性数组,并将其与Array.filter()
进行比较方法