有没有办法找出在JavaScript中的所有用户定义的窗口属性和变量(全局变量)?
Is there a way to find out all user defined window properties and variables (global variables) in javascript?
我试过的console.log(窗口)但名单是无止境的。
I tried console.log(window) but the list is endless.
推荐答案您需要做的工作为自己。阅读所有属性,在第一次的时间就可以了。从这一点上,你可以用静态的比较属性列表。
You would need to do the work for yourself. Read in all properties, on the first possible time you can. From that point on, you can compare the property list with your static one.
var globalProps = [ ]; function readGlobalProps() { globalProps = Object.getOwnPropertyNames( window ); } function findNewEntries() { var currentPropList = Object.getOwnPropertyNames( window ); return currentPropList.filter( findDuplicate ); function findDuplicate( propName ) { return globalProps.indexOf( propName ) === -1; } }所以,现在,我们可以去像
So now, we could go like
// on init readGlobalProps(); // store current properties on global object和后来
window.foobar = 42; findNewEntries(); // returns an array of new properties, in this case ['foobar']当然,这里需要说明的是,你只能在那里你的脚本能够调用它的最早时间时间冻结在全球属性列表。
Of course, the caveat here is, that you can only "freeze" the global property list at the time where your script is able to call it the earliest time.
更多推荐
获取所有用户自定义窗口的属性?
发布评论