9

Possible Duplicate:
Fetching all (javascript) global variables in a page

My application is using global variables in javascript. Is there a way to find how many of them are there?

Thanks Om

Community
  • 1
  • 1
Ohm
  • 143
  • 2
  • 7
  • 6
    The easiest way would be to inspect the `window` object. – Shmiddty Dec 28 '12 at 00:24
  • Out of curiosity, why do you want to set global variables? I'm trying to think of a case where that would be necessary, but I can't come up with one. – Thomas Dec 28 '12 at 00:42
  • @Thomas: When we designed the application initially we used it and now i am trying to remove and make them local.. – Ohm Dec 28 '12 at 06:01
  • @Ohm - can you clarify... do you want to get a list of all global vars, or just the ones that you're creating? I read this question as "how can I tell which global vars are mine so I can get rid of them" in which case I think the mods closed the question prematurely. – Thomas Jan 02 '13 at 21:20

5 Answers5

14

I made one.

var GlobalTester = (function(){
    var fields = {};
    var before = function(w){
        for(var field in w){
            fields[field] = true;
        };
    };

    var after = function(w){
        for(var field in w){
            if(!fields[field]){
                 console.log(field + " has been added");
            }            
        };

    };
    return {before: before, after:after};
}());

GlobalTester.before(window);

// Run your code here        
window.blar = "sdfg";      

GlobalTester.after(window);        
​
​

This will output blar has been added in the console

david
  • 17,925
  • 4
  • 43
  • 57
8

Try this in your browser developer window (F12):

Object.keys(window).length
Mitch Denny
  • 2,095
  • 13
  • 19
  • 3
    +1 for [`Object.keys`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys) – Paul S. Dec 28 '12 at 00:36
  • 3
    This will return a count of all of the properties of this window, not just the ones that the OP is interested in. – Will C. Dec 28 '12 at 00:38
  • Closed so I can't answer; Also look at using [`Object.getOwnPropertyNames(window)`](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames) if any of the globals you're adding might not be enumerable. – Paul S. Dec 28 '12 at 00:40
  • Ah - no worries. I might have misread the question. I thought he wanted to know how many global variables there were. Not exactly how many his code was using. I'd probably look to JSLint/JSHint for that. – Mitch Denny Dec 28 '12 at 00:41
1

Iterate through the window element like this:

for(var globe in window){
 console.log(globe);
}
Travis J
  • 81,153
  • 41
  • 202
  • 273
0

Using a linter would warn you when globals are introduced. You could also compare items of window before and after your code runs.

ryan
  • 6,541
  • 5
  • 43
  • 68
-2

Inspect the window object, but you will need to know all of the global variable names before you do this, here is an example:

var myGlobalVars = {"global1":0, "global2":0};

function countGlobals() {
    var count = 0;
    for (myGlobalVar in myGlobalVars) {
        if (myGlobalVar in window) {
            count++;
        }
    }

    return count;
}

countGlobals();
Will C.
  • 599
  • 3
  • 8
  • After thinking about it, I understand your concern, since you can just count the number of properties in the globalVars variable. But as I interpreted this question, the properties may or may not be there, so this answer is justified. – Will C. Dec 28 '12 at 00:41
  • 2
    If the OP knows the names of all their global variables, why would they ask the question? – Shmiddty Dec 28 '12 at 16:27
  • In case the global variables are allocated at some unknown point during run-time, but the names of the variables are known during "compile" time. – Will C. Dec 28 '12 at 22:29