Troubleshooting JavaScript global variables

When working on large projects, not all developers use JSLint with strict settings.  This can become a problem if a variable which is presumed to be local is actually global.

I wrote a test script, executable from a JavaScript Console such as FireBug, to echo the list of all global variables.

(function () {
    var allAttrs = [], thisAttr;
    for (thisAttr in window) {
        allAttrs.push(thisAttr);
    }
     console.log(allAttrs);
})();

Advertisement

About this entry