Exterminate Globals!
/
___
D>=G===' '.
|======|
|======|
)--/]IIIIII]
|_______|
C O O O D
C O O O D
C O O O D
C__O__O__O__D
[_____________]
Quickly discover if your JS code is leaking any unwanted globals (we all miss a var
now and then).
Include the src/ExterminateGlobals.js
or build/ExterminateGlobals.min.js
file in your HTML and call ExterminateGlobals.startCollecting()
before any other JavaScript code gets included or executed. Then call ExterminateGlobals.collect();
after all JavaScript has finished executing (you can of course do that manually, from the console). This will then generate a nice report in your browser's console:
After that start hunting down your unwanted globals and exterminate them.
Not to worry, the ExterminateGlobals.startCollecting()
, as its first argument, accepts an array of names (strings) of known globals which will be ignored when generating the report.
Why of course, just pass the object you want to monitor as the second argument to ExterminateGlobals.startCollecting()
.
Besides startCollecting()
and collect()
the ExterminateGlobals
also contains a constructor function GlobalsCollector
(which the convenience functions startCollecting&collect use internally). This means you can monitor multiple objects at the same time or just obtain the list of unwanted globals as an array instead of printing everything out to the console.
Usage example:
var globalsCollector = new ExterminateGlobals.GlobalsCollector( ['$', 'jQuery'], myObject);
globalsCollector.startCollecting();
// your code here
var unwantedGlobals = globalsCollector.collect();
globalsCollector.print(); // pass ExterminateGlobals.PRINT_COMPACT for more compact report
If you use the ExterminateGlobals.GlobalsCollector
directly you get one extra feature. If you call saveUnwantedGlobals(true)
on your object before you start collecting the globals, it will save the list of found globals to localStorage
. Then if you refresh the page and start collecting again, GlobalsCollector
will insert break points which will get hit whenever your code is writing to a global variable. This makes finding globals even easier, as you are shown directly where in your code you've made a mistake.
Look at the ExterminateGlobals.js
file for full API documentation.
Jasmine is used for testing. To run the tests just load the spec/SpecRunner.html
file in your browser.
Tests pass in the following browsers:
Licensed under MIT. See LICENSE.md
file for details.