Your anonymous functions are bad, and maybe we can fix that.

Using anonymous functions for certain callbacks in JS is an age old practice, but it’s high time we all started doing things a little different

If you’ve worked in javascript more than a handful of times, you’ve probably seen something like this:

[javascript]
$elements.each(function (i, el) {
var $el = $(el);
$el.removeClass(‘mod-no-pointer’).addClass(‘mod-label’);
$el.append($(‘<span class="icon icon-standard img-replace"></span>’));
});
[/javascript]

This is technically valid code, and has been a common practice for some time. the $.each() method EXPECTS a function, so how else are we to do this? It gets even more complicated when we Continue reading “Your anonymous functions are bad, and maybe we can fix that.”