有没有办法在 javascript 中找出所有用户定义的窗口属性和变量(全局变量)?
我尝试了 console.log(window)
但列表是无穷无尽的。
最佳答案
您还可以将窗口与干净版本的窗口进行比较,而不是尝试在运行时进行快照以进行比较。我在控制台中运行了它,但是您可以将它变成一个函数。
// make sure it doesn't count my own properties
(function () {
var results, currentWindow,
// create an iframe and append to body to load a clean window object
iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
// get the current list of properties on window
currentWindow = Object.getOwnPropertyNames(window);
// filter the list against the properties that exist in the clean window
results = currentWindow.filter(function(prop) {
return !iframe.contentWindow.hasOwnProperty(prop);
});
// log an array of properties that are different
console.log(results);
document.body.removeChild(iframe);
}());
关于javascript - 获取所有用户定义的窗口属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24820423/