2

I'm trying to tidy up some javascript code and one of the steps is removing all useless (or plain wrong) global variables that have slipped in from errors like:

for (prop in obj) { ...

instead of

for (var prop in obj) { ...

JSLint helps a bit in finding out this nastiness, but it is not 100% foolproof when the nastiness happens at runtime. I already tried to add some monitoring code that routinely checks the global scope logging to the console if some new variable is detected, and that helped some more, but when it tells me that a new global variable named "i" has been detected ... well, it's a mess finding out where that happened in thousands of lines of code.

So here we come: is there a better way/tool/script/whatever to find the little pests? My dream is something like a Firebug plugin that stops the execution whenever a new global variable is created...

Thanks!

hugomg
  • 68,213
  • 24
  • 160
  • 246
Sergio
  • 1,139
  • 14
  • 24

5 Answers5

3

You may find this bookmarklet useful.

Also, checkout this answer: How to detect creation of new global variables?

Community
  • 1
  • 1
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Thanks, but what you suggest is basically what I already do with my monitoring code (i.e.: polling the global scope). What I am really looking for is something more like a conditional breakpoint on the definition of a global variable. I'm well aware that the answer to that may be "not possible" but, well hope die last :) – Sergio Aug 08 '11 at 16:44
  • Yeah, window object is a special object. Polling is I think the only option. – Mrchief Aug 08 '11 at 17:03
2

You can now intercept variable definition as explained on this similar question

window.__defineSetter__('sneakyVariable', function() {
    debugger
})

and you'll be able to find where it was defined

Gabriel Furstenheim
  • 2,969
  • 30
  • 27
0

I wonder if you could set a timeout to create a list of all global variables and then compare that against the last time the timeout fired. I found this on Stack Overflow, and maybe you could use this code in conjunction with a setTimeout() to get what you want.

Blockquote Yes and no. "No" in almost every situation. "Yes," but only in a limited manner, if you want to check the global scope. Take the following example: var a = 1, b = 2, c = 3;

for ( var i in window ) {
    console.log(i, typeof window[i], window[i]);
}

Stack Overflow link: Getting All Variables In Scope

Community
  • 1
  • 1
Darren Griffith
  • 3,290
  • 3
  • 28
  • 35
  • Thanks, but this is what I said I already do which is polling the global scope for new variables. – Sergio Aug 08 '11 at 16:45
0

well, I wrote this long time ago, so code sucks, but it does the job: https://gist.github.com/1132193 paste in the firebug console or include as a script.

wildcard
  • 7,353
  • 3
  • 27
  • 25
0

You say, you are trying to tidy up some code. In that case - use IDE, like NetBeans PHP (free) or JetBrains WebStorm (30$). They both color global variables, and do lots of other useful stuff ;) If your polling script will still detect creation of global variables - trace down offending functions, and make them suffer ;) Eventually, the code will become clean.

c69
  • 19,951
  • 7
  • 52
  • 82