I came across some code at work as follows:
var $divs = $('div'); var $jq = $([1]); $divs.each(function () { $jq.context = $jq[0] = this;
I perf'd higher, and it seems a lot faster than $this = $(this); at the top of the function. Inside the function, the $jq variable has a standard set of typical jQuery methods pushed from it ( .hasClass(), .data(), .find(), ... ), and the code works as intended.
I wonder what the trade-offs are; that is, what do you lose without creating a new object every time? Am I just losing the “story” (ie .pushStack(), .end(), .andSelf(), ... )? Should I do this all the time?
Also, is this a named template? I couldn’t understand all my life how to research this on Google.
source share