Howdy, jwg.
The benefit is explained either in an earlier lesson or on the DVD; I imagine the latter, as it can be better explained with that medium. But for a quick and dirty explanation, it was used for performance reasons. Event utility methods are typically written like this:
Code:
addEvent : function(obj, evt, fn) {
if (typeof addEventListener === "function") {
// use standards-based code
} else {
// use legacy IE code
}
}
It works, but the JavaScript engine has to evaluate the condition (the addEventListener test) every time addEvent() executes. It's a very small performance hit, but a hit none-the-less.
By using a self-executing function, the engine only has to evaluate the condition once at load-time. Since it returns a browser-specific function for adding an event handler, the resulting addEvent() executes as fast as possible.