Caching objects in variables or in $(this).data
I was wondering what could be the better approach. This:
var strips = $('.strip');
for (var i = 0; i &l开发者_StackOverflowt; strips.length; i++) {
(function() {
var strip = $(strips[i]);
var controls = $('.controls', strip);
controls.click(function() {
alert(strip.attr('id'));
}
})();
}
Or this:
$('.strips .controls').click(function() {
var me = $(this);
if (!me.data('strip')) { me.data('strip', me.parents('.strip')) }
alert(me.data('strip').attr('id'));
}
Is there really a difference besides personal preference?
精彩评论