I started a thread like this some time ago for the PHP area, but I think this should be a fun topic for js as well. Share your favorite small functions and objects.
This first example requires prototype.js
var EventHandler = Class.create({
initialize: function() {
for ( m in this ) {
if ( m.substring(0,2) == '__' && typeof(this[m]) == 'function' ) {
this[m.substring(2)] = this[m].bindAsEventListener(this);
}
}
}
});
If you’ve ever created a class that has a lot of listener objects in it this will be useful as a superclass to extend off of as it does the work of binding your event listeners for you. All you have to do is prefix your event handling functions with __. The init function above then parses those functions out into bound listeners.
Any other contributions?