1

In JavaScript in the browser global variables are stored as members of the window host object.

But also in window are all the properties of window which are part of the browser DOM and, if I assume correctly, other global functions and objects which are also host objects or otherwise part of the implementaton / environment provided by the browser.

How can I iterate through the members of window and filter out as much as possible everything that is not just a regular global variable created by code such as var foo = 1;?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • not easy since it is very easy for one to override a custom function or variable with the same name as a builtin function (not very good but it can be done , even by mistake) – Nikos M. Apr 26 '15 at 08:05
  • That's OK. An answer that included that could be more insightful than one that didn't mention it. – hippietrail Apr 26 '15 at 08:06
  • Before I posted this question I couldn't find a prior on SO. But now I find a couple, but each includes other details such as wanting only those prefixed by underscore or only those which are arrays, so in form at least this one seems to be more canonical. It seems wrong to make it a dupe of one of the old questions whose answers have distracting stuff in the code in their answers. [(a)](https://stackoverflow.com/questions/8369338), [(b)](http://stackoverflow.com/questions/2226007) – hippietrail Apr 26 '15 at 08:21
  • It would be handy if you say why you want to do this. In general, it's impossible to determine how a variable comes into being. If that was a useful or common thing to want to do, it would have been made possible. – RobG Apr 26 '15 at 08:48
  • In my case I'm trying to add functionality to a 3rd party webapp using a userscript with Tapermonkey. I know where the code I want to patch is, and I'm trying to find if and whether it's exposed to my userscript inside any of the global variables the webapp creates. It's really too specific to include in my question for something that surely has any number of use cases. – hippietrail Apr 26 '15 at 09:04
  • you will need either a white-list of properties to check (or a black-list of properties to ignore) – Nikos M. Apr 26 '15 at 10:25

1 Answers1

1

Why don't you try:

keys(window);

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • And it filters out all the built in / host object stuff?? Hmm maybe it does. In a blank Chrome browser tab I get just 34 members of `window`: `["top", "window", "location", "external", "chrome", "document", "DoodleNotifier", "google", "_", "closure_lm_744166", "jsl", "closure_memoize_cache_", "__jsaction", "gbar", "gbar_", "__PVT", "gapi", "___jsl", "closure_uid_161476544", "closure_lm_36582", "drasil", "osapi", "gadgets", "iframer", "shindig", "pos", "googleapis", "ToolbarApi", "iframes", "IframeBase", "Iframe", "IframeProxy", "IframeWindow", "__gapi_jstiming__"]` – hippietrail Apr 26 '15 at 08:00
  • To some this might appear a newbie question but you can also do lots of js programming without ever needing to get introspective like this, so a brief bit about what kind of filtering is done / not done and the key terminology so we know what to look for to learn more about this would be great to include in an answer. – hippietrail Apr 26 '15 at 08:05
  • 1
    Some are added by browser and some by your plugins on the page – Tushar Apr 26 '15 at 08:06
  • Yes I expected that even after filtering there might be some stuff I wasn't interested in so it still seems OK. – hippietrail Apr 26 '15 at 08:15
  • 2
    How does this filter anything other than inherited properties from *Window.prototype* (assuming the host implements prototype inheritance for *window*)? In Safari, there are over 120 properties on *window*, any globals will be mixed in with them. – RobG Apr 26 '15 at 08:45