Javascript Static Variable / Closure

Still having fun with this mad JS stuff!

var alt = (function(){
    var hide = false;
    return function() { return (hide = !hide); }
})();

All you have is functions, but they are made so powerful! “var hide” is an example of an equivalent of a static type variable in C/C++/Java/etc., but it is also completely hidden by the returned inner function [which is the only thing that retains a reference to it in memory (==this is what is meant by ‘Closure’)], and “alt” is a self initializing function referenced by a variable. Crazy as. Love it!

Then you can just use this whenever you need an alternating boolean like maybe for colouring a table list:

backgroundColour = alt() ? #000 : #fff;

for alternating black and white in this case, or wha73v3r. I was using this in an exercise that alternated changing lines of text to upper case or lower case from a stream, if you need another example.