Neat trick for for loops that sets the iterated variable for you:
for (var i = 0, o; o = all[i]; i++) {
if(o.href === "bleh")...
}
Same as above, but speed up large loops by not getting the length attribute every time:
for(var i = 0, len = arr.length; obj = arr[i], i < len; i++){
if(obj.href === 'bleh')...
}
Namespacing and singleton:
// create a namespace
if(typeof(Yourmom) == 'undefined') Yourmom = {};
// now your singletons can be namespaced
Yourmom.bleh = {
init: function(){
// do some stuff
}
};
Equality: if you really wanna check that 2 things are equal, both in value and in type, use the "threequal". Check out Douglas Crockford's "Javascript: The Good Parts" for more.
var a = "something"; var b = "something" if(a === b) // a & b are equal in value and type
Add load event gracefully:
Yourmom.addLoadEvent = function(f) {
var old = window.onload
if (typeof old != 'function') window.onload = f
else { window.onload = function() { old(); f() }}
}
Yourmom.addLoadEvent(Yourmom.bleh.init)
