Wednesday, December 21, 2016

$(document).ready() add how these kids don't know from grep-ability

On a code review with a tad of jQuery, I got dinged for using the old-school
$(document).ready(myFunction);
rather than the new hotness of
$(myFunction);
The former is being deprectated but I really think the recommended syntax suffers from poor grep-ability, you can't readily find all instances of "something that happens when the page loads" across a codebase, or even in the context of a single file, since $(myFunction) is about the same as $(mySelector) and both would require a fancier regex to distinguish usage from $("anotherSelector") or $('yetAnotherSelector').

The problem is that what was the selector for "ready()" never mattered, so this page claims people were using $(), $("document"), $(document), as well as confused people using $("someImageSelector") in the false hope it would wait til the image loaded.

In practice, I only saw $(document) used, but anyway. Progress... Better syntactics, poorer searching.

No comments:

Post a Comment