Javascript - 转储所有全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8369338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript - dumping all global variables
提问by DuckDucking
Is there a way in Javascript to get a list or dump the contents of all global variables declared by Javascript/jQuery script on a page? I am particularly interested in arrays. If I can get the array names, it will be enough to me. Seeing its values is a bonus.
在 Javascript 中有没有办法在页面上获取列表或转储由 Javascript/jQuery 脚本声明的所有全局变量的内容?我对数组特别感兴趣。如果我能得到数组名称,对我来说就足够了。看到它的价值是一种奖励。
回答by RightSaidFred
Object.keys( window );
This will give you an Array of all enumerableproperties of the window
object, (which are global variables).
这将为您提供对象的所有可枚举属性的数组window
(它们是全局变量)。
For older browsers, include the compatibility patch from MDN.
对于较旧的浏览器,请包含MDN 中的兼容性补丁。
To see its values, then clearly you'll just want a typical enumerator, like for-in
.
要查看其值,显然您只需要一个典型的枚举器,例如for-in
.
You should note that I mentioned that these methods will only give you enumerableproperties. Typically those will be ones that are not built-in by the environment.
你应该注意到我提到这些方法只会给你可枚举的属性。通常,那些不是环境内置的。
It is possible to add non-enumerableproperties in ES5 supported browsers. These will not be included in Object.keys
, or when using a for-in
statement.
可以在 ES5 支持的浏览器中添加不可枚举的属性。这些不会包含在Object.keys
, 或使用for-in
语句时。
As noted by @Raynos, you can Object.getOwnPropertyNames( window )
for non-enumerables. I didn't know that. Thanks @Raynos!
正如@Raynos所指出的,您可以 Object.getOwnPropertyNames( window )
针对不可枚举。我不知道。谢谢@Raynos!
So to see the values that include enumerables, you'd want to do this:
因此,要查看包含可枚举项的值,您需要执行以下操作:
var keys = Object.getOwnPropertyNames( window ),
value;
for( var i = 0; i < keys.length; ++i ) {
value = window[ keys[ i ] ];
console.log( value );
}
回答by robocat
The following function only dumps global variables that have been addedto the window object:
以下函数仅转储已添加到 window 对象的全局变量:
(function(){
//noprotect <- this comment prevents jsbin interference
var windowProps = function() {
// debugger;
var result = {};
for (var key in window) {
if (Object.prototype.hasOwnProperty.call(window, key)) {
if ((key|0) !== parseInt(key,10)) {
result[key] = 1;
}
}
}
window.usedVars = result;
};
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = 'about:blank';
document.body.appendChild(iframe);
var fwin = iframe.contentWindow;
var fdoc = fwin.document;
fdoc.open('text/html','replace');
fdoc.write('<!DOCTYPE html><body><script>window.onload = ' + windowProps.toString() + '<\u002Fscript>');
fdoc.close();
var button = document.createElement('input');
button.type = 'button';
button.value = 'go';
document.body.appendChild(button);
button.onclick = function() {
var clean = fwin.usedVars;
windowProps();
var dirty = window.usedVars;
for (var key in clean) {
delete dirty[key];
}
for (var variable in dirty) {
var div = document.createElement('div');
div.textContent = variable;
document.body.appendChild(div);
}
document.body.removeChild(button);
document.body.removeChild(iframe);
};
})();
It works by using an iframe to get a clean list of global window variables, then comparing that with the list of global variables in the current window. It uses a button because the iframe runs asynchronously. The code uses a global variable because that makes the code easier to understand.
它的工作原理是使用 iframe 获取干净的全局窗口变量列表,然后将其与当前窗口中的全局变量列表进行比较。它使用按钮是因为 iframe 异步运行。代码使用全局变量,因为这使代码更易于理解。
You can see it working hereor here, although note that these examples show many global variables "leaked" by jsbin itself (different depending on which link you use).
您可以在此处或此处看到它的工作情况,但请注意,这些示例显示了 jsbin 本身“泄漏”的许多全局变量(根据您使用的链接而有所不同)。
回答by pimvdb
Since all global variables are properties of the window
object, you can get them using:
由于所有全局变量都是window
对象的属性,您可以使用以下方法获取它们:
for(var key in window) { // all properties
if(Array.isArray(window[key])) { // only arrays
console.log(key, window[key]); // log key + value
}
}
Since all default/inherited properties are not plain arrays (mostly host objects or functions), the Array.isArray
check is sufficient.
由于所有默认/继承的属性都不是普通数组(主要是宿主对象或函数),因此Array.isArray
检查就足够了。
回答by c-smile
To get "globals" object you can use this function:
要获取“全局”对象,您可以使用此函数:
function globals() { return this; }
Here is the test: http://jsfiddle.net/EERuf/
这是测试:http: //jsfiddle.net/EERuf/
回答by Ry-
window
is the global object in a browser, and you can use a for..in
loop to loop through its properties:
window
是浏览器中的全局对象,您可以使用for..in
循环来遍历其属性:
if(!Array.isArray) {
Array.isArray = function(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
for(var x in window) {
if(Array.isArray(window[x])) {
console.log('Found array ' + x + ' in ' + window + ', it has the value ' + window[x] + '!');
}
}
回答by Remek Ambroziak
You can use npm package called get-globals. It compares properties of window
with fresh-created iframe
to print only variables declared by dev(s), not browser vendor.
您可以使用名为get-globals 的npm 包。它将window
与新创建的属性进行比较,以iframe
仅打印由 dev(s) 而非浏览器供应商声明的变量。
回答by BugaBuga
Greasymonkey script to get leaked globals
获取泄露的全局变量的 Greeasymonkey 脚本
// ==UserScript==
// @name SCI
// @namespace ns
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
console.log('SCI loaded');
var SCI = window.SCI = {
defprops: [],
collect: function(){
var wprops = [];
for(var prop in window){
wprops.push(prop);
}
return wprops;
},
collectDef: function(){
this.defprops = this.collect();
},
diff: function(){
var def = this.defprops,
cur = this.collect();
var dif = [];
for(var i = 0; i < cur.length; i++){
var p = cur[i];
if(def.indexOf(p) === -1){
dif.push(p);
}
}
return dif;
},
diffObj: function(){
var diff = this.diff();
var dobj = {};
for (var i = 0; i < diff.length; i++){
var p = diff[i];
dobj[p]=window[p];
}
return dobj;
}
};
SCI.collectDef();
To use run in console SCI.diff()
to get list of names or SCI.diffObj()
to get object with variables
在控制台中使用 runSCI.diff()
来获取名称列表或SCI.diffObj()
获取带有变量的对象