JQuery: why create a variable so that you only reference once?

So for something like this:

Jquery:

$(document).ready(function(){
    var container = $('.rotate_container');
    var images = container.find('img');
    container.everyTime(10, function (){
        images.each(function() {
            container.animate({scrollTop: $(this).offset().top - 165}, 2000).delay(1000);
        });
    });
});

What is the point of making variables "container" and "images"? If they are used only once, how does it help at all?

thank.

+1
source share
3 answers

If you, for sure , only get access to an element once in your code, it does not make sense to cache it, of course.

The reason for caching DOM elementsis that it is terribly expensive to handle.

bridge ECMAland DOMland - . , , , . ECMAland, . , .

, , .

+5

images.each container.find('img'). , 3 (.everytime,.find('img') .animate).

0

. , , .. DOM . , , .

0
source

All Articles