获取所有用户自定义窗口的属性? [英] Get all user defined window properties?

查看:162
本文介绍了获取所有用户自定义窗口的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找出在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.

这篇关于获取所有用户自定义窗口的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆