Occasionally, however, even closures won’t help you trap a given variable. Case in point:
|var a = b = 0;|
If you’re familiar with languages like PHP, you might think this simple closure creates two variables with the same value, but you’d be wrong. It creates a local variable,
a and a global variable
b, both of which have their value set to 0.
|$a = $b = 0;|
global declaration or the
var are added to the global namespace. Hence the namespace pollution in the above example.
Revisiting the closure, it’s best to rewrite it in one of two ways to maintain the variable scope:
|var a = 0, b = 0;|
|var a = 0, b = a;|
Which solution will work best is dependent solely on context. If you’re minifying the code and the value being assigned is anything more than a single character, the latter is probably the way to go.
head of your document) and it does the rest. It will attempt to report its findings to the console (if your browser has one) or it will create a floating notice at the top of the page.
You won’t want to include this script on a production site and it’s still pretty basic, but it could be very useful for tracking down any accidental emissions in your script.