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

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

问题描述

有没有办法在javascript中找出所有用户定义的窗口属性和变量(全局变量)?

Is there a way to find out all user defined window properties and variables (global variables) in javascript?

我尝试了 console.log(window) 但列表是无穷无尽的.

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;
    }
}

所以现在,我们可以像

// 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天全站免登陆