I know the standard way of doing it:
$('div').each(function(){
// here `this` is bound to DOM Element
// use $(this) to access the jQuery wrapper
})'
But this is a little bit cumbersome because we need to use $(this) everywhere and this causes a performance penalty as shown by http://jsperf.com/jquery-each-this.
I am looking for a way to iterate over a jQuery array/selector with this bound to the jQuery wrapper instead of to the DOM element.
You can use an ordinary loop. and use
.slice(index, 1)to get the corresponding jQuery object..eq()does the same thing, and maps to.slice(), so.slice(i, 1)is more efficient than.eq(index).Notice that the closure is not present. If you want to use closures, create a temporary function:
Update: A jQuery plugin to achieve your desired “each-syntax”: