0

In the window object there are variables and other properties and constructors like Array, Object etc).

Is there any possibility to list the global variables that were created by a script?

e.g.

// after including jQuery
foo() // => ["$", "jQuery"]

My first assumption would be to create a list with properties created by the browser:

var bList = ["Object", "Array" ...];

And then we can do:

var v = [];
for (var k in window) {
    if (bList.indexOf(k) === -1) {
        v.push(k);
    }
}
return v;

Also, this will not catch the case when a property is overridden.

Is there any better solution?

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 1
    possible duplicate of [Fetching all (javascript) global variables in a page](http://stackoverflow.com/questions/2226007/fetching-all-javascript-global-variables-in-a-page) – Jeremy J Starcher Aug 04 '14 at 19:33
  • Well, that may sound like a stupid idea, but maybe you need to make some kind of associative array just before all other scripts are loaded, and in each element the key is window property name, and the value is it's serialized state? – Ilya Luzyanin Aug 04 '14 at 19:33
  • @JeremyJStarcher Actually, that gives me all the globas by passing a prefix. I need all globals that are not added by the browser. – Ionică Bizău Aug 04 '14 at 19:44
  • @IlyaLuzyanin That's my assumption too. Something like `bList = Object.keys(window)`, before any scripts load. – Ionică Bizău Aug 04 '14 at 19:45
  • Yes.. but: Run that *first* -- before anything else happens, with a prefix of `""`. Then, run, load your scripts, and then run that script again at the end. Look for differences. – Jeremy J Starcher Aug 04 '14 at 19:45

0 Answers0