The jQuery selectors are great, but sometimes I find that I am typing them again and again, and this is a little annoying.
$('#mybutton').click(function() { $('#message-box').doSomething(); $('#message-box').doSomethingElse(); $('#message-box').attr('something', 'something'); });
So often, I like to cache my objects in variables:
$('#mybutton').click(function() { var msg = $('#message-box'); msg.doSomething(); msg.doSomethingElse();
Are there any pros or cons between these two patterns? Sometimes it seems that creating variables is an extra job, but sometimes it allows me to type a lot of fingers. Are there any memory issues you need to know about? After the selectors clear well after use, whereas my bad coding habits tend to keep the vars in memory longer?
It doesn't hold me back at night, but I'm curious. Thanks.
EDIT: see this question . It essentially sets the same thing, but I like the answer better.
performance javascript jquery
Bryan M.
source share