I just had this really dumb bug that took 20m to find simply because I forgot the var
keyword somewhere. I figured it out, but it just got me thinking...
"Is there an easy way to see if any properties were added to the global object?"
Edit: Sorry, forgot to mention the context is in Node.js.
ES5’s strict mode prevents this! Just put a 'use strict'
statement at the top of any function or source file:
'use strict';
function foo() {
var x = 5 // Forgot the comma!
y = 6; // Fails in strict mode as an implicit global
}
foo();
Linters should also check for this, and you should use both a linter and strict mode, ideally. I recommend JSHint.